I wanted to override an internal call in a base class and keep the same interface and general algorithm. The changes depend on the state of the object and thus can't just be set once in the constructor. Is there a way to get this to work? Or perhaps a design pattern for this?
#include <iostream>
using namespace std;
class X
{
private:
virtual void internalCall()
{
cout << "Class X" << endl;
}
public:
X()
{
internalCall();
}
void externalCall()
{
internalCall();
}
};
class Y : public X
{
protected:
virtual void internalCall2()
{
cout << "Class Y2" << endl;
}
virtual void internalCall()
{
cout << "Class Y" << endl;
internalCall2();
}
public:
Y() : X() {}
};
int main()
{
Y y;
y.externalCall();
return 0;
}
Desired output:
Class Y
Class Y2
Class Y
Class Y2
Actual output:
Class X
Class Y
Class Y2
On my machine:
Class X
Class X