Re: Virtual base class constructor
Sorry, Ben!
What I quote is not what you mentioned, but what MSDN mentioned. Let me ask
in another way,
Here is the related statement from MSDN and here is my code.
1. Does my code reflects what MSDN mentioned?
2. What is the purpose of "vtordisp" fields and how it is used?
From MSDN virtual base class reference, what means "If a derived class
overrides a virtual function that it inherits from a virtual base class, and
if a constructor or a destructor for the derived base class calls that
function using a pointer to the virtual base class, the compiler may
introduce additional hidden "vtordisp" fields into the classes with virtual
bases."
I do not understand why an additional hidden filed is needed?
http://msdn2.microsoft.com/en-us/library/wcz57btd.aspx#Mtps_DropDownFilterText
[Code]
#include <iostream>
using namespace std;
class Base {
private:
int i;
public:
Base()
{
i = 100;
}
virtual void foo()
{
cout << "Base " << endl;
}
};
class Derived1 : virtual public Base {
private:
int j;
public:
Derived1()
{
Base* pb = this;
pb->foo(); // call Base class's virtual function inside derived class's
constructor by using pointed to base type
j = 200;
}
virtual void foo()
{
cout << "Derived1 " << endl;
}
};
class Derived2 : virtual public Base {
private:
int k;
public:
Derived2()
{
k = 300;
}
virtual void foo()
{
cout << "Derived2 " << endl;
}
};
class Final : public Derived1, public Derived2 {
private:
int t;
public:
Final()
{
t = 400;
}
virtual void foo()
{
cout << "Final " << endl;
}
};
int main()
{
Final f;
return 0;
}
[/Code]
regards,
George