Syed Babu

Hi guys,

I'm a software developer from india working on C++ since 6 years. I have question regarding virtual inheritance.

Consider the following code

class A
{
public:
A()
{
a = 0;
}
public:
virtual void Print()
{
cout<<"a = "<<a<<endl;
}
private:
int a;
};
//************************************************************
class B : virtual public A
{
public:
B()
{
b = 0;
}
virtual void Print()
{
cout<<"b = "<<b<<endl;
}
protected:
int b;
};
//************************************************************
void main()
{
B obj;
}

I'm trying to figure out the memory model for the object B. I'm just giving you the values given by my computer.

&obj = ff6c [ Binary equivalen of address ff6c: 65388 ]

&obj.a = ff70 [ 65392 ]

&A::_vfptr = ff78 [ 65400]

&A::a = ff7c [ 65404]

Now I have 2 questions:

1. What lies in between the ff70 and ff78 as you there is 4 bytes gap. [64396].

I think this is the A::vbptr -> virtual base pointer. But I'm not very sure.

2. What actually lies in the address 65388 Because obj.a lies in a different address 65392.

If any one of you might have gone through the Inside C++ Object model by lipman or C++ Inside the Hood by jan gray can answer this.



Re: Visual C++ Language Virtual Inheritance with a virutal function

Viorel.

In Visual Studio 2005, I obtained a different result:

&obj: 0x0012ff3c

&obj.b 0x0012ff40

&obj.a 0x0012ff48

&obj.__vfptr 0x0012ff44

(A*)&obj 0x0012ff44

&((A*)&obj)->__vfptr 0x0012ff44

&b+1 0x0012ff4c

i.e. it seems there is no gap. What compiler are you using





Re: Visual C++ Language Virtual Inheritance with a virutal function

Jonathan Caves - MSFT

Here is what Visual C++ 2008 thinks the layout of the class B is:

class B size(20):
+---
0 | {vbptr}
4 | b
+---
8 | (vtordisp for vbase A)
+--- (virtual base A)
12 | {vfptr}
16 | a
+---

Thr vtordisp is an offset that is used if you call any virtual functions in the constructor and destructor.






Re: Visual C++ Language Virtual Inheritance with a virutal function

Red Swingline Stapler

Just out of curiosity, what is your struct member alignment set to in project settings





Re: Visual C++ Language Virtual Inheritance with a virutal function

Syed Babu

Sorry i did a mistake in memory layout.

It is some thing like this.

&obj = FF6C [ 65388 ]

&( obj.b ) = FF70 [65392 ]

&(obj.A ) = FF78 [65400 ]

&(obj.__vfptr) = FF78 [65400 ]

This is obvious that the entry point of A will be same as that of entry point of the vptr.

&(obj.A::a) = FF7C [65404 ]

My question what actually lies in the address 65388 i.,e the 4 bytes 65388-65391

You have mentioned two vfptrs in you answer and it is wrong.

It will have only one vfptr derived from the class A and whose address is being overwritten by B.

So it will have only one vfptr.

B class will have 2 vfptr's when it has its own virtual functions.

I think the address FF74 has the vbptr as B class is virtually derived from A.

I think i'm right.

But my big question about what lies in the address FF6C





Re: Visual C++ Language Virtual Inheritance with a virutal function

Jonathan Caves - MSFT

See my response above - it is the pointer to the virtual base table.




Re: Visual C++ Language Virtual Inheritance with a virutal function

Syed Babu

Can you explain what is vtordisp for vbase A

How this is used

Will this be available in all the inherited classes

Or it will be availble in the virtualy inherited classes where the base class has the virtual function

In which situation we will have this

Is it related to the adjuster thunks

Where can i get more information on the vtordisp





Re: Visual C++ Language Virtual Inheritance with a virutal function

Jonathan Caves - MSFT

This should help http://msdn2.microsoft.com/en-us/library/ms879782.aspx

It is used very rarely - but we have to add it to classes that inherit from a virtual base class and override virtual functions just in case the user does call a virtual function in the constructor or destructor.

[ Interesting - if you search on google for vtordisp this thread is the 6th hit - just shows you how rare vtordisp is :-) ]






Re: Visual C++ Language Virtual Inheritance with a virutal function

Syed Babu

Hi jonathan

Thanks for your reply and the information is very useful.

Can i get your email id if you dont mind

Thanks