Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   sizeof(derived) >= sizeof(base)? (http://www.velocityreviews.com/forums/t754686-sizeof-derived-sizeof-base.html)

cppquester 10-07-2011 09:41 AM

sizeof(derived) >= sizeof(base)?
 
Although I am pretty sure this is true:
Is it guaranteed, that the size (from sizeof operator) of a derived
class is always at least as large as the base class?
Thanks,
Marc

Juha Nieminen 10-07-2011 11:02 AM

Re: sizeof(derived) >= sizeof(base)?
 
cppquester <cppquester@googlemail.com> wrote:
> Although I am pretty sure this is true:
> Is it guaranteed, that the size (from sizeof operator) of a derived
> class is always at least as large as the base class?


I don't know what the standard says, so this is complete speculation.
However, I would imagine that it makes no such guarantee.

A plausible (but in practice completely hypothetical) situation where
the derived class might actually be smaller than the base class is when
so-called empty base class optimization is in play. If the compiler
decides that the size of an empty class is, let's say, 4 bytes, while
the size of a class containing one char is 1 byte, you could stumble
across this very situation, if the latter is derived from the former.

I'm pretty certain this will never happen in practice, though.


All times are GMT. The time now is 11:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.