I have a unique problem I am trying to solve and am unable to find a good solution. I am aware this might not be the best architecture but I am exploring solutions.
Let's say I have a base class, and two child classes inheriting from this base class. The child classes have a mix of unique functions that all have the same function prototype (same return value and arguments).
Class Base
{
Base(){}
}
Class Child : Base
{
Child(){}
void FunctionA(int Arg1, int Arg2){};
void FunctionB(int Arg1, int Arg2){};
}
Class WildChild : Base
{
WildChild(){}
void FunctionC(int Arg1, int Arg2){};
void FunctionD(int Arg1, int Arg2){};
}
Is there any way to define member function pointers such that the Base class could use these function pointers, even though the Base class doesn't implement the corresponding functions?
The way I understand it is that the child classes need to implement their own function pointer definitions and could populate a table of function pointers...
For Child
typedef void (Child::*ChildFunctionPtrType)(int Arg1, int Arg2);
ChildFunctionPtrType funcPtrTable[2];
funcPtrTable[0] = &Child::FunctionA;
funcPtrTable[1] = &Child::FunctionB;
For WildChild
typedef void (WildChild::*WildChildFunctionPtrType)(int Arg1, int Arg2);
WildChildFunctionPtrType funcPtrTable[2];
funcPtrTable[0] = &WildChildFunctionPtrType::FunctionC;
funcPtrTable[1] = &WildChildFunctionPtrType::FunctionD;
But what I really need is a function pointer table that could be used by the Base class but contain pointers to member functions in the Child classes.
typedef void (Base::*FunctionPtrType)(int Arg1, int Arg2);
Class Base
{
Base(){}
FunctionPtrType funcPtrTable[SIZE_OF_TABLE];
}
Child::PopulateTable
{
funcPtrTable[0] = &Child::FunctionA;
funcPtrTable[1] = &Child::FunctionB;
}
WildChild::PopulateTable
{
funcPtrTable[0] = &WildChild::FunctionC;
funcPtrTable[1] = &WildChild::FunctionD;
}
Is this possible? If not, is this possible using templates?