Hi.
I'm trying to pass a pointer to a function in a class as a parameter to another class' constructor, but I'm not sure how to do this.
I created a small example-program:
Code:
#include <iostream>
using std::cout;
using std::endl;
class A
{
public:
A(int n) { this->n = n; }
~A() {}
int funcA() { return n; };
private:
int n;
};
class B
{
public:
B(int (*func)()) { cout << func() << endl;}
~B() {}
};
int main()
{
A* a = new A(1338);
B* b = new B(a->funcA);
return 0;
}
The output from g++:
test.cpp: In function `int main()':
test.cpp:27: error: no matching function for call to `B::B(<unknown type>)'
test.cpp:18: error: candidates are: B::B(const B&)
test.cpp:20: error: B::B(int (*)())
What I want it to do is to print "1338" when it's run.
What am I doing wrong?
Thanks in advance.