Suppose I have the following code
class A{
public:
void f1()
{
/*
* Other functions that happen here.
* These calls needs to be made at A's f1() function
* And cannot be in derived class.
*/
f2();
}
virtual void f2()
{
std::cout << "I am base class" << std::endl;
}
};
class B : A
{
public:
void f2()
{
std::cout << "I am derived class" << std::endl;
}
void f3()
{
A::f1();
}
};
int main()
{
B b;
b.f3();
return 0;
}
The output above is
I am derived class
The limitation I have here is that I can't change class A in any way. Another limitation is that f2() needs to be called through f1(). A::f3() cannot call f1(). The call to f1() needs to be made to A since A's f1() does other things and B cannot override it.
Is there a way to force A::f2() be called when B::f3() is called? In other words, when we ask for the base version, how do we enforce all other functions called within the base class, to use the base version. In this example, that would be f2().
The question marked as duplicate is simply asking to call a parent function from a derived function with no other functions in the called function that overwritten.
Please note that a static cast of A to call f1 will result in the derived class's f2 function to be called.