Reputation: 2545
I have some base class in which I store a pointer to a function from derived class. I need to call a function by its pointer from two places: from BaseClass and from derived class.
template < class T >
class BaseClass {
private:
typedef void ( T::*FunctionPtr ) ();
FunctionPtr funcPtr;
public:
void setFunc( FunctionPtr funcPtr ) {
this->funcPtr = funcPtr;
( this->*funcPtr )(); // I need to call it here also but it doesn't work
}
};
class DerivedClass: public BaseClass < DerivedClass > {
public:
void callMe() {
printf( "Ok!\n" );
}
void mainFunc() {
setFunc( &DerivedClass::callMe );
( this->*funcPtr )(); // works fine here
}
};
Error: left hand operand to -> * must be a pointer to class compatible with the right hand operand, but is 'BaseClass *'
Upvotes: 0
Views: 1146
Reputation: 206567
( this->*funcPtr )();
is the wrong syntax to use to call funcPtr
since the type of funcPtr
is void T::*()
.
You need to use:
( (static_cast<T*>(this))->*funcPtr )();
Upvotes: 4