Re: Why sizeof(struct) is different in C and C++ ?

From: Alf P. Steinbach (alfps_at_start.no)
Date: 03/27/04


Date: Sat, 27 Mar 2004 12:58:01 GMT


* "Steven T. Hatton" <susudata@setidava.kushan.aa> schriebt:
> Old Wolf wrote:
>
> > My conceptual view is a stack of base classes, eg.
> > |-|-------|-----|
> > |A| B | C |
> > |-|-------|-----|
> > nothing wrong with one of them being blank :)
>
> If it has virtual functions, then it must have a virtual function table with
> RTTI (I believe). Will that not cause the baseclass to be of non-zero
> size?

Caution: the following apply only for practical C++ implementations; virtual
function tables & pointers to them are _not_ part of the language definition.

For "ordinary" inheritance where each class inherits from exactly one immediate
base class (which in turn inherits..., and so on), the answer is no.

All the classes in the inheritance chain then define a memory layout with room
for just a single virtual function table pointer.

When you instantiate an object of class C first class A's constructor set that
vtable pointer to point to class A's vtable. Then class B's constructor
changes the pointer to point to class B's vtable, which is layout-compatible
with class A's. Then class C's constructor changes the pointer to point to
class C's vtable, which is layout-compatible with class B's.

So the (single inheritance) object has only a single vtable pointer, and it
"belongs" to all the classes in the inheritance chain.

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Relevant Pages

  • Re: Suspected memory leak in ATLCOM.h in CComCreator::CreateInstan
    ... it contains the pointer to one of its base classes. ... Either BoundsChecker or your tracking code are wrong. ... inheritance to one of the base classes, ...
    (microsoft.public.vc.atl)
  • sizeof(object) is different in ANSI and Unicode
    ... A class having no member variables and only a method sizeof ... I have the answer for this of how in works in ANSI. ... form a pointer to an empty object that is distinct from a pointer to ... For a class with virtual function, size of a virtual function is a ...
    (comp.lang.cpp)
  • Re: CListCtrl, custom item data and allocation policy
    ... This is a good example of where to use inheritance. ... CMyListCtrl use DWORDs and then inherit from CMyListCtrl and have it use ... The problem is it takes a DWORD argumentr that can reference a value or a ... pointer to an allocated structure. ...
    (microsoft.public.vc.mfc)
  • Re: is it good, is it bad or plain eeevil ?
    ... > This is a virtual function call regardless of how call_vfun was called ... If sword is 'given' to player (that is pointer is passed), ... virtual method. ...
    (comp.lang.cpp)
  • Re: Would a static_cast be better style here?
    ... >> And of course to convert from a derived pointer type to a base pointer ... > pointer, then you are correct, there is no need to cast. ... For a virtual function the scope resolution operator is the only way. ...
    (comp.lang.cpp)