Now how about a class’s function pointer to another class’s function? That’s my question
Example:
class Apple
{
int(Apple::*pActionFunc)(void);
};
class Tree
{
Apple* tempy;
void Init( void );
int I_Work( void );
};
void Tree::Init( void )
{
tempy = new Apple( );
tempy->pActionFunc = &Tree::I_Work;
}
int Tree::I_Work( void )
{
"Do Things"
}
That will not work. Why is that?
This will not work, because like all variables function pointers also have an type and you can only use an pointer to same type to store address of an variable type.
Tells the compiler that
pActionFuncis a pointer to an member function ofAppleclass which takesvoidas an parameter and returns anint.Tries to store the address of an function
I_Work()which is member ofTreeclass an which takesvoidas input parameter and returns anint.If you notice they are members functions of different classes and hence the error.