Re: Why sizeof(struct) is different in C and C++ ?
From: Alf P. Steinbach (alfps_at_start.no)
Date: 03/27/04
- Next message: Rolf Magnus: "Re: The End of C++"
- Previous message: John Harrison: "Re: operator overload"
- In reply to: Steven T. Hatton: "Re: Why sizeof(struct) is different in C and C++ ?"
- Next in thread: Steven T. Hatton: "Re: Why sizeof(struct) is different in C and C++ ?"
- Reply: Steven T. Hatton: "Re: Why sizeof(struct) is different in C and C++ ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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?
- Next message: Rolf Magnus: "Re: The End of C++"
- Previous message: John Harrison: "Re: operator overload"
- In reply to: Steven T. Hatton: "Re: Why sizeof(struct) is different in C and C++ ?"
- Next in thread: Steven T. Hatton: "Re: Why sizeof(struct) is different in C and C++ ?"
- Reply: Steven T. Hatton: "Re: Why sizeof(struct) is different in C and C++ ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|