I understand the principle of why you can only call base class functions when assigning a derived class to a base class pointer. From my understanding of virtual tables the reason why is because only base class virtual function pointers are put into the virtual table so only they can be called but if the base class pointer points to a derived class, when the derived class constructor is called the base class virtual function is replaced with the override in the virtual table.
However my point is: are there going to be problems if you have a non virtual member function in the derived class that is used in a derived class override of a virtual base class member function.
My theory is there shouldn't because its a question of function access not jumps within the function, by assigning the pointer to new DerivedClass, you are assigning space for all the derived class, so access to the virtual overridden function is allowed using the pointer to the virtual function kept in the v-table. All actions from there onward are as if you were just in a derived class object so it can jump to whatever function it wants.
Is this correct? Or is there an issue with the jump in the function using the base class pointer as a jump reference (like an array: *pointer + 2 for function), is that how member functions are called?
If this is (not my intention but still) no issue, could it be theoretically possible to have a virtual function that takes, say an int argument. Then a switch statement inside the derived class override of the virtual function that, depending on int passed in, calls specific derived class functions thereby working around the 'only call base class defined virtual functions' problem?
eg. (skeleton with no constructors etc. and separation of declaration/definition)
class A
{
protected:
virtual void AFunc() ;
} ;
class B : public A
{
public:
virtual void AFunc() { BSpecificFunc() ;} // doable?
/* if thats the case why not:
virtual void Sfunv(int temp)
{
switch(temp)
{
case 1 : BSpecificFunca() ; break;
case 2: BSpecificFuncb() ; break;
etc.
}
}
*/
private:
void BSpecificFunc() ;
void BSpecificFunca() ;
void BSpecificFuncb() ;
void BSpecificFuncc() ;
} ;
int main()
{
A *pBaseClass = new B;
pBaseClass->AFunc(); //Will there be any issues doing this?
}
currently at design stage which is why I have kept this nice and theoretical.