innews:03bb4d45-72ff-4d8d-bee0- (e-mail address removed):
On 30/01/2011 16:14, Serve Laurijssen wrote:
[..]
In VC++ the vtable pointer will be in the
RefCounted base sub-object so will not be affected by the memset.
Really? I would have expected that it be in front of the
UnitHeader element, with another vptr in the RefCounted
subclass. (The vptr for CHeader has to point to a different
vtable than that of RefCounted.)
No, it doesn't as vtable is inherited only from one branch of the
multiple inheritance tree. VS2010 keeps the vtable pointer in the
RefCounted subobject and fills it in with CHeader virtual function
pointers (destructor only in this example).
So I see. But to do this, it rearranges the object, so that the
CHeader subobject has the same address as the most derived
object. Without this rearrangement (which is perfectly valid),
it would need a vptr for the CHeader, and another for the most
derived object, since code will access the vptr from the address
it has.
I'm not sure what you mean here - CHeader is not a subobject, it is the
most derived class in this example. And in the VStudio debugger I do not
see it has rearranged anything, all subobjects are in the same order as
appearing in the code. In particular, the CHeader vtable pointer is
placed in the middle of the CHeader object.