Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is C++ allowed to increase the derived class size if there're no new member variables compared to the base class?

Suppose I have a base class with some member variables and no virtual functions:

class Base {    int member; }; 

and a derived class that derives in a non-virtual way from Base and has no new member variables an again no virtual functions:

class Derived : Base { }; 

Obviously sizeof(Derived) can't be smaller than sizeof(Base).

Is sizeof(Derived) required to be equal to sizeof(Base)?

like image 873
sharptooth Avatar asked Nov 13 '13 09:11

sharptooth


People also ask

How do you determine the size of a derived class?

@John5342 - sizeof(DerivedClass1) must include BaseClass::a and BaseClass::b . Those members must exist. The fact that their relative positions may be different in any class that inherits from DerivedClass1 doesn't change that. sizeof(DerivedClass1) includes all members, even those inherited via virtual inheritance.

Can a derived class have more than one base class?

You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance.

What allows a class to be derived from an existing class?

In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes. Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class).

Can a derived class become a base class?

Inheritance enables you to create new classes that reuse, extend, and modify the behavior defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class.


2 Answers

From 5.3.2 [expr.sizeof]

When applied to a class, the result [of sizeof] is the number of bytes in an object of that class including any padding required for placing objects of that type in an array. The size of a most derived class shall be greater than zero (1.8).

From 1.8 [intro.object]

Unless it is a bit-field (9.6), a most derived object shall have a non-zero size and shall occupy one or more bytes of storage. Base class sub-objects may have zero size. An object of POD type (3.9) shall occupy contiguous bytes of storage.

and a note:

The actual size of a base class subobject may be less than the result of applying sizeof to the subobject, due to virtual base classes and less strict padding requirements on base class subobjects.

Put these together and I think what it's telling you is that you have no guarantees whatsoever as to what sizeof might tell you, other than the result will be greater than zero. In fact, it doesn't even seem to guarantee that sizeof(Derived) >= sizeof(Base)!

like image 104
Tristan Brindle Avatar answered Sep 22 '22 02:09

Tristan Brindle


There is no such requirement.

The only relevant part of the language I can think of is that every object, whether complete or not, and whether most-derived or not, has an identity, which is given by the pair of its address and its type. Cf. C++11 1.8/6:

Two objects that are not bit-fields may have the same address if one is a subobject of the other, or if at least one is a base class subobject of zero size and they are of different types; otherwise, they shall have distinct addresses.

So both the most-derived object and the base subobject of your example must have distinct identities.

It would certainly make sense for a compiler to give both Base and Derived a size of 1, but this is not mandatory. It would be acceptable if the Base had size 1729 and Derived had size 2875.

like image 44
Kerrek SB Avatar answered Sep 21 '22 02:09

Kerrek SB