Topic: Q: Sizeof and inheritance


Author: Barry Margolin <barmar@bbnplanet.com>
Date: 1998/03/15
Raw View
In article <6eakfj$5hk$1@news1.enator.se>,
Mikael T yr  <mikael.toyra@enator.se> wrote:
>The function sizeof can calculate the size of an object of some type of
>class, but can it handle object's of a class that use inheritance?
>
>Ex. the CBaseClass my contain 2 int and the CSubClass 2 int.
>CBaseClass pBase = new CSubClass();

Shouldn't that be:

CBaseClass *pBase = new CSubClass();

>sizeof(pBase)=?

The result of sizeof is based on the static class of the parameter, not the
dynamic class; it's computed at compile time.

Also, in your example, you're asking for the size of a pointer, not the
object it points to.  Did you mean sizeof(*pBase)?  This will be equivalent
to sizeof(CBaseClass).

--
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Mikael T yr " <mikael.toyra@enator.se>
Date: 1998/03/13
Raw View
The function sizeof can calculate the size of an object of some type of
class, but can it handle object's of a class that use inheritance?

Ex. the CBaseClass my contain 2 int and the CSubClass 2 int.
CBaseClass pBase = new CSubClass();
sizeof(pBase)=?

How intelligente is sizeof?

Please send a copy to:
mikael.toyra@enator.se
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]