Misunderstanding function pointer - passing it as an argument
c++, function-pointers
Solution
Pointers to members are different from normal function pointers. As the compiler error indicates the type of `&A::dosomeA` is actually `int (A::*)(int)` and not `int (*)(int)`.
Inside `B`'s constructor you need an instance of `A` to call the member on using one the `.*` or `->*` operators.
E.g'
B(int(A::*ptr)(int))
{
A atmp;
(atmp.*ptr)(int);
}
Problem
I want to pass a member function of class A to class B via a function pointer as argument. Please advise whether this road is leading somewhere and help me fill the pothole. ``` #include <iostream> using namespace std; class A{ public: int dosomeA(int x){ cout<< "doing some A to "<<x <<endl; return(0); } }; class B{ public: B(int (*ptr)(int)){ptr(0);}; }; int main() { A a; int (*APtr)(int)=&A::dosomeA; B b(APtr); return 0; } ``` This brilliant piece of code leaves me with the compiler error: cannot convert `int (A::*)(int)' to`int (*)(int)' in initialization Firstly I want it to compile. Secondly I don't want dosomeA to be STATIC.