C++ Function Pointer Problems
Hello,
I'm having problems understanding/using function pointers. I'm trying to use a member function of an instantiated class i.e. non-static as a call back function and need to pass a function pointer to the caller.
I've posted an example below which will result in the same compile error in my code :
Code:
class MyClass {
public:
MyClass(){};
virtual ~MyClass(){};
int add(int oneNumber, int twoNumber){return (oneNumber + twoNumber);};
};
int main(){
MyClass* t = new MyClass();
int (*fp)(int,int); // Create a function pointer - will point to a member function of an INSTANCE of class MyClass
fp = t->add; // Results in a compiler error
delete(t);
}
The compiler gives :
Quote:
test.cpp:12: error: argument of type ‘int (MyClass::)(int, int)’ does not match ‘int (*)(int, int)
Just how do I pass a (*) pointer ? (and what is a (*) pointer anyway ???) Unfortunately I cannot change the calling function as it is code not written by myself and it is has been written in C. How do I pass a pointer to a function of a class instance ?
I'm using Linux by the way and gcc/g++ compiler
Thanks