So, while being in a ctor/dtor of a base class while doing smth with a derived class and calling member functions (including virtual), whether via this pointer or not, the function of the relevant class will be called.
How come? Is vtable pointer of the object being altered somehow during the process? Because, as I may understand it, there is generally a single vtable pointer in an object unless multiple inheritance is used.
I have the following code to exemplify what I mean:
#include <stdio.h>
class B {
public:
B()
{ printf("B constructor!\n"); f(); g(); }
virtual ~B()
{ printf("B destructor!\n"); f(); g(); }
virtual void f()
{ printf("f() in B!\n"); }
void g()
{ printf("g() in B!\n"); }
void h()
{ printf("h() in B!\n"); }
};
class D : public B {
public:
D()
{ printf("D constructor!\n"); f(); g(); }
virtual ~D()
{ printf("D destructor!\n"); f(); g(); }
virtual void f()
{ printf("f() in D!\n"); }
void g()
{ printf("g() in D!\n"); h(); }
};
int main()
{
D *d = new D;
B *b = d;
delete b;
}
In ctors/dtors, the member functions of created/destructed object is called.