A
Alf P. Steinbach /Usenet
* Virchanza, on 05.07.2011 23:58:
Yes.
See the FAQ, <url:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.5>.
Or your favorite C++ textbook.
Cheers & hth.,
- Alf
I've been playing around with GCC, and it seems as though GCC changes
an object's V-Table pointer while the object is constructed from the
ground up (from Base class to furthermost Derived class).
Here's the code I tried:
#include<iostream>
using std::cout;
class Base {
private:
static void Call_Init(Base *const p)
{
p->Init();
}
public:
virtual void Init()
{
cout<< "This is Base::Init\n";
}
Base()
{
Call_Init(this);
}
};
class Derived : public Base {
public:
virtual void Init()
{
cout<< "This is Derived::Init\n";
}
};
int main()
{
Base a;
Derived b;
}
The output from this program is:
This is Base::Init
This is Base::Init
I think there's two possible explanations for this:
1) The "Call_Init" function is expanded inline and is not actually
consulting the object's V-Table pointer.
2) The "Call_Init" function does actually consult the object's V-Table
pointer, but when Call_Init is called, the object's V-Table pointer
points to the V-Table for the Base Class. Presumably then, the
object's V-Table pointer gets updated as each derived class undergoes
construction.
Is Number 2 the norm for all C++ compilers?
Yes.
See the FAQ, <url:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.5>.
Or your favorite C++ textbook.
Cheers & hth.,
- Alf