I am trying to understand why, and if it is possible to call child method from parent constructor.
First I tried:
#define prn printf(__FUNCTION__);printf("\r\n");
class A
{
public:
A(){init();}
~A(){prn;}
virtual void f(){}
virtual void init()=0;
};
class B : public A<B>
{
public:
B(){prn;}
~B(){prn;}
virtual void init(){prn;}
};
Which crushed with pure virtual call. I can guess its because init() in A() points to init() in A's virtual table. With this line I confirmed it:
virtual void init()=0{prn};
So, I tried the following:
template<typename T>
class A
{
public:
A(){((T*)this)->init();}
~A(){prn;}
virtual void f(){}
virtual void init()=0{prn;}
};
class B : public A<B>
{
public:
B(){prn;}
~B(){prn;}
virtual void init(){prn;}
};
Which also crushed! Now my only guess is that it happens because the virtual table is just being made, but its just a guess...
Can anyone explain what is going on?
Thanks!