Files
cppdraft_translate/cppdraft/class/derived/general.md
2025-10-25 03:02:53 +03:00

10 KiB
Raw Blame History

[class.derived.general]

11 Classes [class]

11.7 Derived classes [class.derived]

11.7.1 General [class.derived.general]

1

#

A list of base classes can be specified in a class definition using the notation:

base-clause:
base-specifier-list

base-specifier-list:
base-specifier ...opt
base-specifier-list , base-specifier ...opt

base-specifier:
attribute-specifier-seqopt class-or-decltype
attribute-specifier-seqopt virtual access-specifieropt class-or-decltype
attribute-specifier-seqopt access-specifier virtualopt class-or-decltype

class-or-decltype:
nested-name-specifieropt type-name
nested-name-specifier template simple-template-id
computed-type-specifier

access-specifier:
private
protected
public

The optional attribute-specifier-seq appertains to the base-specifier.

2

#

The component names of a class-or-decltype are those of itsnested-name-specifier,type-name, and/orsimple-template-id.

A class-or-decltype shall denote a (possibly cv-qualified) class type that is not an incompletely defined class ([class.mem]); any cv-qualifiers are ignored.

The class denoted by the class-or-decltype of a base-specifier is called adirect base class for the class being defined; for each such base-specifier, the corresponding direct base class relationship is the ordered pair (D, B) where D is the class being defined andB is the direct base class.

The lookup for the component name of the type-name or simple-template-id is type-only ([basic.lookup]).

A class B is a base class of a class D if it is a direct base class ofD or a direct base class of one of D's base classes.

A class is an indirect base class of another if it is a base class but not a direct base class.

A class is said to be (directly or indirectly) derived from its (direct or indirect) base classes.

[Note 1:

See [class.access] for the meaning ofaccess-specifier.

— end note]

Members of a base class are also members of the derived class.

[Note 2:

Constructors of a base class can be explicitly inherited ([namespace.udecl]).

Base class members can be referred to in expressions in the same manner as other members of the derived class, unless their names are hidden or ambiguous ([class.member.lookup]).

The scope resolution operator :: ([expr.prim.id.qual]) can be used to refer to a direct or indirect base member explicitly, even if it is hidden in the derived class.

A derived class can itself serve as a base class subject to access control; see [class.access.base].

A pointer to a derived class can be implicitly converted to a pointer to an accessible unambiguous base class ([conv.ptr]).

An lvalue of a derived class type can be bound to a reference to an accessible unambiguous base class ([dcl.init.ref]).

— end note]

3

#

The base-specifier-list specifies the type of thebase class subobjects contained in an object of the derived class type.

[Example 1: struct Base {int a, b, c;};

struct Derived : Base {int b;};

struct Derived2 : Derived {int c;};

Here, an object of class Derived2 will have a subobject of classDerived which in turn will have a subobject of classBase.

— end example]

4

#

A base-specifier followed by an ellipsis is a pack expansion ([temp.variadic]).

5

#

The order in which the base class subobjects are allocated in the most derived object ([intro.object]) is unspecified.

[Note 3:

A derived class and its base class subobjects can be represented by a directed acyclic graph (DAG) where an arrow means “directly derived from” (see Figure 3).

An arrow need not have a physical representation in memory.

A DAG of subobjects is often referred to as a “subobject lattice”.

— end note]

SVG Image
Figure 3 — Directed acyclic graph [fig:class.dag]

6

#

[Note 4:

Initialization of objects representing base classes can be specified in constructors; see [class.base.init].

— end note]

7

#

[Note 5:

A base class subobject can have a layout different from the layout of a most derived object of the same type.

A base class subobject can have a polymorphic behavior ([class.cdtor]) different from the polymorphic behavior of a most derived object of the same type.

A base class subobject can be of zero size; however, two subobjects that have the same class type and that belong to the same most derived object cannot be allocated at the same address ([intro.object]).

— end note]