Consider the following base class, which will count how many times I call CallDerivedFunction:
class B {
public:
B() {numCalls = 0;}
void CallDerivedFunction() {numCalls++; DerivedFunction();}
int GetNumCalls() {return numCalls;}
private:
virtual void DerivedFunction() = 0;
int numCalls;
};
People other than me will be writing derived classes that implement DerivedFunction. However, I'm trying to count how many times DerivedFunction gets called.
Is there any way prevent a derived class D from directly calling its own implementation of DerivedFunction, eg. disallow:
class D : public B {
private:
void DerivedFunction() {
if(<some condition>) DerivedFunction(); // I don't want to allow this.
if(<some condition>) CallDerivedFunction(); // This is OK.
}
};
Note: OK, of course this is a simplification; the more general question is, when my base class is supposed to be functioning as a manager and doing things like tracking usage patterns, etc for a collection of derived classes, can I force the derived classes to call through the manager class? Or must I rely on derived-class developers to do the right thing here?