Hi all,
I know you shouldnt use virtual functions inside constructors because the vtable has yet to be constructed. However I have come across a problem which im convinced is a common problem so Im hoping there is a well known way to solve it.
So if class A is my base class which requires all derived classes to implement function Init and then to make sure Init is never forgotten to be called it calls Init from its own constructor. Now I know the above code will not work because before B is constructed it constructs A so there is no way that A can call B's version of Init because the vtable hasnt been setup.Code:class A { public: A( ) { Init() ; } virtual void Init() = 0 ; }; class B : public A { public: virtual void Init() {} };
Is there a way to solve the above problem so that it is gaurenteed than anything that derives from class A will always be initialized on construction?
Thanks for any help