sizeof(derived) >= sizeof(base)?

C

cppquester

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
 
J

Juha Nieminen

cppquester said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top