Misunderstanding function pointer - passing it as an argument

Posted by Stef on Stack Overflow See other posts from Stack Overflow or by Stef
Published on 2010-03-19T08:29:05Z Indexed on 2010/03/19 8:31 UTC
Read the original article Hit count: 150

Filed under:
|

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)' toint (*)(int)' in initialization

Firstly I want it to compile.
Secondly I don't want dosomeA to be STATIC.

© Stack Overflow or respective owner

Related posts about c++

Related posts about function-pointers