Alexander
Alexander

Reputation: 2591

What do they mean by the expressions "of the same type" highlighted below in [class.derived]/7?

[class.derived]/7:

[Note: A base class subobject might have a layout ([basic.stc]) different from the layout of a most derived object of the same type. A base class subobject might have a polymorphic behavior ([class.cdtor]) different from the polymorphic behavior of a most derived object of the same type. A base class subobject may be of zero size ([class]); however, two subobjects that have the same class type and that belong to the same most derived object must not be allocated at the same address ([expr.eq]). — end note]

Upvotes: 1

Views: 103

Answers (2)

Language Lawyer
Language Lawyer

Reputation: 3568

struct B { /* ... */ };
struct D : B { /* ... */ };

B b;
D d;

The base class subobject B of d may have different layout than the most derived object (b here) of the same type (B). For example, an implementation may reuse the tail padding of B for D's members.

Upvotes: 2

AlanK
AlanK

Reputation: 1974

Are you looking for something deeper than the obvious

class Animal {} // base
class Mammal : Animal {} // derived
class Dog : Mammal {} // most derived

Animal, Dog and Mammal are all type Animal.

Bearing the possibility of multiple inheritance in mind (e.g. you could have another hierarchy containing AquaticOrganism and TerrestrialOrganism and choose to define Dog as both (inheriting from) Mammal and TerrestrialOrganism), the passage is pointing out that a Dog cannot be assumed to have layout "Animal plus stuff" (it might be "TerrestrialOrganism plus...").

Upvotes: -1

Related Questions