Files
2025-10-25 03:02:53 +03:00

180 lines
10 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[class.derived.general]
# 11 Classes [[class]](./#class)
## 11.7 Derived classes [[class.derived]](class.derived#general)
### 11.7.1 General [class.derived.general]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3511)
A list of base classes can be specified in a class definition using
the notation:
[base-clause:](#nt:base-clause "11.7.1General[class.derived.general]")
: [*base-specifier-list*](#nt:base-specifier-list "11.7.1General[class.derived.general]")
[base-specifier-list:](#nt:base-specifier-list "11.7.1General[class.derived.general]")
[*base-specifier*](#nt:base-specifier "11.7.1General[class.derived.general]") ...opt
[*base-specifier-list*](#nt:base-specifier-list "11.7.1General[class.derived.general]") , [*base-specifier*](#nt:base-specifier "11.7.1General[class.derived.general]") ...opt
[base-specifier:](#nt:base-specifier "11.7.1General[class.derived.general]")
[*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt [*class-or-decltype*](#nt:class-or-decltype "11.7.1General[class.derived.general]")
[*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt virtual [*access-specifier*](#nt:access-specifier "11.7.1General[class.derived.general]")opt [*class-or-decltype*](#nt:class-or-decltype "11.7.1General[class.derived.general]")
[*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt [*access-specifier*](#nt:access-specifier "11.7.1General[class.derived.general]") virtualopt [*class-or-decltype*](#nt:class-or-decltype "11.7.1General[class.derived.general]")
[class-or-decltype:](#nt:class-or-decltype "11.7.1General[class.derived.general]")
[*nested-name-specifier*](expr.prim.id.qual#nt:nested-name-specifier "7.5.5.3Qualified names[expr.prim.id.qual]")opt [*type-name*](dcl.type.simple#nt:type-name "9.2.9.3Simple type specifiers[dcl.type.simple]")
[*nested-name-specifier*](expr.prim.id.qual#nt:nested-name-specifier "7.5.5.3Qualified names[expr.prim.id.qual]") template [*simple-template-id*](temp.names#nt:simple-template-id "13.3Names of template specializations[temp.names]")
[*computed-type-specifier*](dcl.type.simple#nt:computed-type-specifier "9.2.9.3Simple type specifiers[dcl.type.simple]")
[access-specifier:](#nt:access-specifier "11.7.1General[class.derived.general]")
private
protected
public
The optional [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") appertains to the [*base-specifier*](#nt:base-specifier "11.7.1General[class.derived.general]")[.](#1.sentence-2)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3554)
The component names of a [*class-or-decltype*](#nt:class-or-decltype "11.7.1General[class.derived.general]") are those of its[*nested-name-specifier*](expr.prim.id.qual#nt:nested-name-specifier "7.5.5.3Qualified names[expr.prim.id.qual]"),[*type-name*](dcl.type.simple#nt:type-name "9.2.9.3Simple type specifiers[dcl.type.simple]"), and/or[*simple-template-id*](temp.names#nt:simple-template-id "13.3Names of template specializations[temp.names]")[.](#2.sentence-1)
A [*class-or-decltype*](#nt:class-or-decltype "11.7.1General[class.derived.general]") shall denote
a (possibly cv-qualified) class type that is not
an incompletely defined class ([[class.mem]](class.mem "11.4Class members"));
any cv-qualifiers are ignored[.](#2.sentence-2)
The class denoted by the [*class-or-decltype*](#nt:class-or-decltype "11.7.1General[class.derived.general]") of
a [*base-specifier*](#nt:base-specifier "11.7.1General[class.derived.general]") is called a[*direct base class*](#def:base_class,direct "11.7.1General[class.derived.general]") for the class being defined;
for each such [*base-specifier*](#nt:base-specifier "11.7.1General[class.derived.general]"),
the corresponding [*direct base class relationship*](#def:relationship,direct_base_class "11.7.1General[class.derived.general]") is the ordered pair (D, B)
where D is the class being defined andB is the direct base class[.](#2.sentence-3)
The lookup for the component name of
the [*type-name*](dcl.type.simple#nt:type-name "9.2.9.3Simple type specifiers[dcl.type.simple]") or [*simple-template-id*](temp.names#nt:simple-template-id "13.3Names of template specializations[temp.names]") is type-only ([[basic.lookup]](basic.lookup "6.5Name lookup"))[.](#2.sentence-4)
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[.](#2.sentence-5)
A class is an [*indirect base class*](#def:base_class,indirect "11.7.1General[class.derived.general]") of another if it is a base
class but not a direct base class[.](#2.sentence-6)
A class is said to be (directly or
indirectly) [*derived*](#def:derived) from its (direct or indirect) base
classes[.](#2.sentence-7)
[*Note [1](#note-1)*:
See [[class.access]](class.access "11.8Member access control") for the meaning of[*access-specifier*](#nt:access-specifier "11.7.1General[class.derived.general]")[.](#2.sentence-8)
— *end note*]
Members of a base class are also members of the derived class[.](#2.sentence-9)
[*Note [2](#note-2)*:
Constructors of a base class can be explicitly inherited ([[namespace.udecl]](namespace.udecl "9.10The using declaration"))[.](#2.sentence-10)
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]](class.member.lookup "6.5.2Member name lookup"))[.](#2.sentence-11)
The scope resolution operator :: ([[expr.prim.id.qual]](expr.prim.id.qual "7.5.5.3Qualified names")) can be used
to refer to a direct or indirect base member explicitly,
even if it is hidden in the derived class[.](#2.sentence-12)
A derived class can itself serve as a base class subject to access
control; see [[class.access.base]](class.access.base "11.8.3Accessibility of base classes and base class members")[.](#2.sentence-13)
A pointer to a derived class can be
implicitly converted to a pointer to an accessible unambiguous base
class ([[conv.ptr]](conv.ptr "7.3.12Pointer conversions"))[.](#2.sentence-14)
An lvalue of a derived class type can be bound
to a reference to an accessible unambiguous base
class ([[dcl.init.ref]](dcl.init.ref "9.5.4References"))[.](#2.sentence-15)
— *end note*]
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3609)
The [*base-specifier-list*](#nt:base-specifier-list "11.7.1General[class.derived.general]") specifies the type of the[*base class subobjects*](#def:base_class_subobjects) contained in an
object of the derived class type[.](#3.sentence-1)
[*Example [1](#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[.](#3.sentence-2)
— *end example*]
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3637)
A [*base-specifier*](#nt:base-specifier "11.7.1General[class.derived.general]") followed by an ellipsis is a pack
expansion ([[temp.variadic]](temp.variadic "13.7.4Variadic templates"))[.](#4.sentence-1)
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3641)
The order in which the base class subobjects are allocated in the most
derived object ([[intro.object]](intro.object "6.8.2Object model")) is unspecified[.](#5.sentence-1)
[*Note [3](#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](#fig:class.dag))[.](#5.sentence-2)
An arrow need not have a physical representation in memory[.](#5.sentence-3)
A DAG of subobjects is often referred to as a “subobject lattice”[.](#5.sentence-4)
— *end note*]
![SVG Image](data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNzlwdCIgdmlld0JveD0iMC4wMCAwLjAwIDc5LjAwIDE0MC4wMCIgaGVpZ2h0PSIxNDBwdCI+CjxnIGNsYXNzPSJncmFwaCI+CmRhZwoKPGcgY2xhc3M9Im5vZGUiPgpCYXNlCkJhc2UKPC9nPgoKPGcgY2xhc3M9Im5vZGUiPgpEZXJpdmVkMQpEZXJpdmVkMQo8L2c+Cgo8ZyBjbGFzcz0iZWRnZSI+CkRlcml2ZWQxLSZndDtCYXNlCjxwYXRoIGZpbGw9Im5vbmUiIGQ9Ik0zNS41LC03Ni40MUMzNS41LC04My4zIDM1LjUsLTkyLjkgMzUuNSwtMTAxLjQzIiBzdHJva2U9ImJsYWNrIiAvPgo8cG9seWdvbiBmaWxsPSJibGFjayIgc3Ryb2tlPSJibGFjayIgLz4KPC9nPgoKPGcgY2xhc3M9Im5vZGUiPgpEZXJpdmVkMgpEZXJpdmVkMgo8L2c+Cgo8ZyBjbGFzcz0iZWRnZSI+CkRlcml2ZWQyLSZndDtEZXJpdmVkMQo8cGF0aCBzdHJva2U9ImJsYWNrIiBkPSJNMzUuNSwtMjAuNDFDMzUuNSwtMjcuMyAzNS41LC0zNi45IDM1LjUsLTQ1LjQzIiBmaWxsPSJub25lIiAvPgo8cG9seWdvbiBmaWxsPSJibGFjayIgc3Ryb2tlPSJibGFjayIgLz4KPC9nPgo8L2c+Cjwvc3ZnPg==)
Figure [3](#fig:class.dag) — Directed acyclic graph [[fig:class.dag]](./fig:class.dag)
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3661)
[*Note [4](#note-4)*:
Initialization of objects representing base classes can be specified in
constructors; see [[class.base.init]](class.base.init "11.9.3Initializing bases and members")[.](#6.sentence-1)
— *end note*]
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3667)
[*Note [5](#note-5)*:
A base class subobject can have a layout different
from the layout of a most derived object of the same type[.](#7.sentence-1)
A base class
subobject can have a polymorphic behavior ([[class.cdtor]](class.cdtor "11.9.5Construction and destruction"))
different from the polymorphic behavior of a most derived object of the
same type[.](#7.sentence-2)
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]](intro.object "6.8.2Object model"))[.](#7.sentence-3)
— *end note*]