I have a base class:
class CBase { public: virtual void SomeChecks() {} CBase() { SomeChecks(); } };
and derived class:
class CDerived : public CBase { public: virtual void SomeChecks() { } CDerived() : CBase() {} };
This design seems a little strange, but in my case it is required because CBase does some checks and CDerived can mix some checks between them. You can see this as a way to hook functions in the constructor. The problem with this design is that when building CDerived, CBase is created first, and there is no awareness of CDerived (so that someChecks () overloaded function is not called).
I could do something like this:
class CBase { public: void Init() { SomeChecks(); } virtual void SomeChecks() {} CBase(bool bDoInit=true) { if (bDoInit) { Init(); } } }; class CDerived : public CBase { public: virtual void SomeChecks() { } CDerived() : CBase(false) { Init() } };
This is not very safe because I want the constructor with a false parameter to be protected, so only derived classes can call it. But then I will have to create a second constructor (which is protected) and force it to accept other parameters (it may not be used because the constructor is called when Init () does not need to be called).
So, I'm completely stuck here.
EDIT Actually, I want something like this:
class CBase { protected: void Init() { } CBase() { } public: CBase() { Init(); }
It seems to me that it is impossible to create 2 constructors with the same arguments that are protected and public?
To1ne source share