From Jason Turner

[class.derived]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp8zwnch72/{from.md → to.md} +78 -80
tmp/tmp8zwnch72/{from.md → to.md} RENAMED
@@ -1,7 +1,9 @@
1
  ## Derived classes <a id="class.derived">[[class.derived]]</a>
2
 
 
 
3
  A list of base classes can be specified in a class definition using the
4
  notation:
5
 
6
  ``` bnf
7
  base-clause:
@@ -36,42 +38,41 @@ access-specifier:
36
  ```
37
 
38
  The optional *attribute-specifier-seq* appertains to the
39
  *base-specifier*.
40
 
41
- A *class-or-decltype* shall denote a (possibly cv-qualified) class type
 
 
42
  that is not an incompletely defined class [[class.mem]]; any
43
  cv-qualifiers are ignored. The class denoted by the *class-or-decltype*
44
  of a *base-specifier* is called a *direct base class* for the class
45
- being defined. During the lookup for a base class name, non-type names
46
- are ignored [[basic.scope.hiding]]. A class `B` is a base class of a
47
- class `D` if it is a direct base class of `D` or a direct base class of
48
- one of `D`’s base classes. A class is an *indirect base class* of
49
- another if it is a base class but not a direct base class. A class is
50
- said to be (directly or indirectly) *derived* from its (direct or
51
- indirect) base classes.
52
 
53
  [*Note 1*: See [[class.access]] for the meaning of
54
  *access-specifier*. — *end note*]
55
 
56
- Unless redeclared in the derived class, members of a base class are also
57
- considered to be members of the derived class. Members of a base class
58
- other than constructors are said to be *inherited* by the derived class.
59
- Constructors of a base class can also be inherited as described in 
60
- [[namespace.udecl]]. Inherited members can be referred to in expressions
61
- in the same manner as other members of the derived class, unless their
62
- names are hidden or ambiguous [[class.member.lookup]].
63
 
64
- [*Note 2*: The scope resolution operator `::` [[expr.prim.id.qual]] can
65
- be used to refer to a direct or indirect base member explicitly. This
66
- allows access to a name that has been redeclared in the derived class. A
67
- derived class can itself serve as a base class subject to access
68
- control; see  [[class.access.base]]. A pointer to a derived class can be
69
- implicitly converted to a pointer to an accessible unambiguous base
70
- class [[conv.ptr]]. An lvalue of a derived class type can be bound to a
71
- reference to an accessible unambiguous base class
72
- [[dcl.init.ref]]. *end note*]
 
 
 
73
 
74
  The *base-specifier-list* specifies the type of the *base class
75
  subobjects* contained in an object of the derived class type.
76
 
77
  [*Example 1*:
@@ -107,29 +108,26 @@ derived object [[intro.object]] is unspecified.
107
 
108
  [*Note 3*: A derived class and its base class subobjects can be
109
  represented by a directed acyclic graph (DAG) where an arrow means
110
  “directly derived from” (see Figure [[fig:class.dag]]). An arrow
111
  need not have a physical representation in memory. A DAG of subobjects
112
- is often referred to as a “subobject lattice”.
113
 
114
  <a id="fig:class.dag"></a>
115
 
116
  ![Directed acyclic graph \[fig:class.dag\]](images/figdag.svg)
117
 
118
- — *end note*]
119
-
120
  [*Note 4*: Initialization of objects representing base classes can be
121
  specified in constructors; see  [[class.base.init]]. — *end note*]
122
 
123
- [*Note 5*: A base class subobject might have a layout [[basic.stc]]
124
- different from the layout of a most derived object of the same type. A
125
- base class subobject might have a polymorphic behavior [[class.cdtor]]
126
- different from the polymorphic behavior of a most derived object of the
127
- same type. A base class subobject may be of zero size [[class]];
128
- however, two subobjects that have the same class type and that belong to
129
- the same most derived object must not be allocated at the same address
130
- [[expr.eq]]. — *end note*]
131
 
132
  ### Multiple base classes <a id="class.mi">[[class.mi]]</a>
133
 
134
  A class can be derived from any number of base classes.
135
 
@@ -147,22 +145,22 @@ class D : public A, public B, public C { ... };
147
 
148
  — *end example*]
149
 
150
  [*Note 2*: The order of derivation is not significant except as
151
  specified by the semantics of initialization by constructor
152
- [[class.base.init]], cleanup [[class.dtor]], and storage layout (
153
- [[class.mem]], [[class.access.spec]]). — *end note*]
154
 
155
  A class shall not be specified as a direct base class of a derived class
156
  more than once.
157
 
158
  [*Note 3*: A class can be an indirect base class more than once and can
159
  be a direct and an indirect base class. There are limited things that
160
- can be done with such a class. The non-static data members and member
161
- functions of the direct base class cannot be referred to in the scope of
162
- the derived class. However, the static members, enumerations and types
163
- can be unambiguously referred to. — *end note*]
164
 
165
  [*Example 2*:
166
 
167
  ``` cpp
168
  class X { ... };
@@ -200,12 +198,12 @@ subobjects of class `L` as shown in Figure [[fig:class.nonvirt]].
200
  <a id="fig:class.nonvirt"></a>
201
 
202
  ![Non-virtual base \[fig:class.nonvirt\]](images/fignonvirt.svg)
203
 
204
  In such lattices, explicit qualification can be used to specify which
205
- subobject is meant. The body of function `C::f` could refer to the
206
- member `next` of each `L` subobject:
207
 
208
  ``` cpp
209
  void C::f() { A::next = B::next; } // well-formed
210
  ```
211
 
@@ -272,25 +270,24 @@ declared with the keyword `virtual` or if it overrides a virtual member
272
  function declared in a base class (see below).[^7]
273
 
274
  [*Note 1*: Virtual functions support dynamic binding and
275
  object-oriented programming. — *end note*]
276
 
277
- A class that declares or inherits a virtual function is called a
278
  *polymorphic class*.[^8]
279
 
280
- If a virtual member function `vf` is declared in a class `Base` and in a
281
- class `Derived`, derived directly or indirectly from `Base`, a member
282
- function `vf` with the same name, parameter-type-list [[dcl.fct]],
283
- cv-qualification, and ref-qualifier (or absence of same) as `Base::vf`
284
- is declared, then `Derived::vf` *overrides*[^9] `Base::vf`. For
285
- convenience we say that any virtual function overrides itself. A virtual
286
- member function `C::vf` of a class object `S` is a *final overrider*
287
- unless the most derived class [[intro.object]] of which `S` is a base
288
- class subobject (if any) declares or inherits another member function
289
- that overrides `vf`. In a derived class, if a virtual member function of
290
- a base class subobject has more than one final overrider the program is
291
- ill-formed.
292
 
293
  [*Example 1*:
294
 
295
  ``` cpp
296
  struct A {
@@ -316,11 +313,11 @@ void foo() {
316
 
317
  ``` cpp
318
  struct A { virtual void f(); };
319
  struct B : A { };
320
  struct C : A { void f(); };
321
- struct D : B, C { }; // OK: A::f and C::f are the final overriders
322
  // for the B and C subobjects, respectively
323
  ```
324
 
325
  — *end example*]
326
 
@@ -390,20 +387,20 @@ A virtual function shall not have a trailing *requires-clause*
390
  [[dcl.decl]].
391
 
392
  [*Example 5*:
393
 
394
  ``` cpp
 
395
  struct A {
396
  virtual void f() requires true; // error: virtual function cannot be constrained[temp.constr.decl]
397
  };
398
  ```
399
 
400
  — *end example*]
401
 
402
- Even though destructors are not inherited, a destructor in a derived
403
- class overrides a base class destructor declared virtual; see 
404
- [[class.dtor]] and  [[class.free]].
405
 
406
  The return type of an overriding function shall be either identical to
407
  the return type of the overridden function or *covariant* with the
408
  classes of the functions. If a function `D::f` overrides a function
409
  `B::f`, the return types of the functions are covariant if they satisfy
@@ -420,14 +417,15 @@ the following criteria:
420
  as or less cv-qualification than the class type in the return type of
421
  `B::f`.
422
 
423
  If the class type in the covariant return type of `D::f` differs from
424
  that of `B::f`, the class type in the return type of `D::f` shall be
425
- complete at the point of declaration of `D::f` or shall be the class
426
- type `D`. When the overriding function is called as the final overrider
427
- of the overridden function, its result is converted to the type returned
428
- by the (statically chosen) overridden function [[expr.call]].
 
429
 
430
  [*Example 6*:
431
 
432
  ``` cpp
433
  class B { };
@@ -448,11 +446,11 @@ struct No_good : public Base {
448
  class A;
449
  struct Derived : public Base {
450
  void vf1(); // virtual and overrides Base::vf1()
451
  void vf2(int); // not virtual, hides Base::vf2()
452
  char vf3(); // error: invalid difference in return type only
453
- D* vf4(); // OK: returns pointer to derived class
454
  A* vf5(); // error: returns pointer to incomplete class
455
  void f();
456
  };
457
 
458
  void g() {
@@ -481,12 +479,12 @@ object (the static type) [[expr.call]]. — *end note*]
481
 
482
  [*Note 4*: The `virtual` specifier implies membership, so a virtual
483
  function cannot be a non-member [[dcl.fct.spec]] function. Nor can a
484
  virtual function be a static member, since a virtual function call
485
  relies on a specific object for determining which function to invoke. A
486
- virtual function declared in one class can be declared a friend (
487
- [[class.friend]]) in another class. — *end note*]
488
 
489
  A virtual function declared in a class shall be defined, or declared
490
  pure [[class.abstract]] in that class, or both; no diagnostic is
491
  required [[basic.def.odr]].
492
 
@@ -595,14 +593,13 @@ void D::f() { ... B::f(); }
595
  Here, the function call in `D::f` really does call `B::f` and not
596
  `D::f`.
597
 
598
  — *end example*]
599
 
600
- A function with a deleted definition [[dcl.fct.def]] shall not override
601
- a function that does not have a deleted definition. Likewise, a function
602
- that does not have a deleted definition shall not override a function
603
- with a deleted definition.
604
 
605
  A `consteval` virtual function shall not override a virtual function
606
  that is not `consteval`. A `consteval` virtual function shall not be
607
  overridden by a virtual function that is not `consteval`.
608
 
@@ -624,12 +621,12 @@ below. — *end note*]
624
  A class is an *abstract class* if it has at least one pure virtual
625
  function.
626
 
627
  [*Note 3*: An abstract class can be used only as a base class of some
628
  other class; no objects of an abstract class can be created except as
629
- subobjects of a class derived from it ([[basic.def]],
630
- [[class.mem]]). — *end note*]
631
 
632
  A pure virtual function need be defined only if called with, or as if
633
  with [[class.dtor]], the *qualified-id* syntax [[expr.prim.id.qual]].
634
 
635
  [*Example 1*:
@@ -663,17 +660,18 @@ struct C {
663
 
664
  [*Note 5*: An abstract class type cannot be used as a parameter or
665
  return type of a function being defined [[dcl.fct]] or called
666
  [[expr.call]], except as specified in [[dcl.type.simple]]. Further, an
667
  abstract class type cannot be used as the type of an explicit type
668
- conversion ([[expr.static.cast]], [[expr.reinterpret.cast]],
669
- [[expr.const.cast]]), because the resulting prvalue would be of abstract
670
- class type [[basic.lval]]. However, pointers and references to abstract
671
- class types can appear in such contexts. *end note*]
 
672
 
673
- A class is abstract if it contains or inherits at least one pure virtual
674
- function for which the final overrider is pure virtual.
675
 
676
  [*Example 3*:
677
 
678
  ``` cpp
679
  class ab_circle : public shape {
@@ -700,11 +698,11 @@ would make class `circle` non-abstract and a definition of
700
  `circle::draw()` must be provided.
701
 
702
  — *end example*]
703
 
704
  [*Note 6*: An abstract class can be derived from a class that is not
705
- abstract, and a pure virtual function may override a virtual function
706
  which is not pure. — *end note*]
707
 
708
  Member functions can be called from a constructor (or destructor) of an
709
  abstract class; the effect of making a virtual call [[class.virtual]] to
710
  a pure virtual function directly or indirectly for the object being
 
1
  ## Derived classes <a id="class.derived">[[class.derived]]</a>
2
 
3
+ ### General <a id="class.derived.general">[[class.derived.general]]</a>
4
+
5
  A list of base classes can be specified in a class definition using the
6
  notation:
7
 
8
  ``` bnf
9
  base-clause:
 
38
  ```
39
 
40
  The optional *attribute-specifier-seq* appertains to the
41
  *base-specifier*.
42
 
43
+ The component names of a *class-or-decltype* are those of its
44
+ *nested-name-specifier*, *type-name*, and/or *simple-template-id*. A
45
+ *class-or-decltype* shall denote a (possibly cv-qualified) class type
46
  that is not an incompletely defined class [[class.mem]]; any
47
  cv-qualifiers are ignored. The class denoted by the *class-or-decltype*
48
  of a *base-specifier* is called a *direct base class* for the class
49
+ being defined. The lookup for the component name of the *type-name* or
50
+ *simple-template-id* is type-only [[basic.lookup]]. A class `B` is a
51
+ base class of a class `D` if it is a direct base class of `D` or a
52
+ direct base class of one of `D`’s base classes. A class is an
53
+ *indirect base class* of another if it is a base class but not a direct
54
+ base class. A class is said to be (directly or indirectly) *derived*
55
+ from its (direct or indirect) base classes.
56
 
57
  [*Note 1*: See [[class.access]] for the meaning of
58
  *access-specifier*. — *end note*]
59
 
60
+ Members of a base class are also members of the derived class.
 
 
 
 
 
 
61
 
62
+ [*Note 2*: Constructors of a base class can be explicitly inherited
63
+ [[namespace.udecl]]. Base class members can be referred to in
64
+ expressions in the same manner as other members of the derived class,
65
+ unless their names are hidden or ambiguous [[class.member.lookup]]. The
66
+ scope resolution operator `::` [[expr.prim.id.qual]] can be used to
67
+ refer to a direct or indirect base member explicitly, even if it is
68
+ hidden in the derived class. A derived class can itself serve as a base
69
+ class subject to access control; see  [[class.access.base]]. A pointer
70
+ to a derived class can be implicitly converted to a pointer to an
71
+ accessible unambiguous base class [[conv.ptr]]. An lvalue of a derived
72
+ class type can be bound to a reference to an accessible unambiguous base
73
+ class [[dcl.init.ref]]. — *end note*]
74
 
75
  The *base-specifier-list* specifies the type of the *base class
76
  subobjects* contained in an object of the derived class type.
77
 
78
  [*Example 1*:
 
108
 
109
  [*Note 3*: A derived class and its base class subobjects can be
110
  represented by a directed acyclic graph (DAG) where an arrow means
111
  “directly derived from” (see Figure [[fig:class.dag]]). An arrow
112
  need not have a physical representation in memory. A DAG of subobjects
113
+ is often referred to as a “subobject lattice”. — *end note*]
114
 
115
  <a id="fig:class.dag"></a>
116
 
117
  ![Directed acyclic graph \[fig:class.dag\]](images/figdag.svg)
118
 
 
 
119
  [*Note 4*: Initialization of objects representing base classes can be
120
  specified in constructors; see  [[class.base.init]]. — *end note*]
121
 
122
+ [*Note 5*: A base class subobject can have a layout different from the
123
+ layout of a most derived object of the same type. A base class subobject
124
+ can have a polymorphic behavior [[class.cdtor]] different from the
125
+ polymorphic behavior of a most derived object of the same type. A base
126
+ class subobject can be of zero size; however, two subobjects that have
127
+ the same class type and that belong to the same most derived object
128
+ cannot be allocated at the same address [[intro.object]]. — *end note*]
 
129
 
130
  ### Multiple base classes <a id="class.mi">[[class.mi]]</a>
131
 
132
  A class can be derived from any number of base classes.
133
 
 
145
 
146
  — *end example*]
147
 
148
  [*Note 2*: The order of derivation is not significant except as
149
  specified by the semantics of initialization by constructor
150
+ [[class.base.init]], cleanup [[class.dtor]], and storage layout
151
+ [[class.mem]], [[class.access.spec]]. — *end note*]
152
 
153
  A class shall not be specified as a direct base class of a derived class
154
  more than once.
155
 
156
  [*Note 3*: A class can be an indirect base class more than once and can
157
  be a direct and an indirect base class. There are limited things that
158
+ can be done with such a class; lookup that finds its non-static data
159
+ members and member functions in the scope of the derived class will be
160
+ ambiguous. However, the static members, enumerations and types can be
161
+ unambiguously referred to. — *end note*]
162
 
163
  [*Example 2*:
164
 
165
  ``` cpp
166
  class X { ... };
 
198
  <a id="fig:class.nonvirt"></a>
199
 
200
  ![Non-virtual base \[fig:class.nonvirt\]](images/fignonvirt.svg)
201
 
202
  In such lattices, explicit qualification can be used to specify which
203
+ subobject is meant. The body of function `C::f` can refer to the member
204
+ `next` of each `L` subobject:
205
 
206
  ``` cpp
207
  void C::f() { A::next = B::next; } // well-formed
208
  ```
209
 
 
270
  function declared in a base class (see below).[^7]
271
 
272
  [*Note 1*: Virtual functions support dynamic binding and
273
  object-oriented programming. — *end note*]
274
 
275
+ A class with a virtual member function is called a
276
  *polymorphic class*.[^8]
277
 
278
+ If a virtual member function F is declared in a class B, and, in a class
279
+ D derived (directly or indirectly) from B, a declaration of a member
280
+ function G corresponds [[basic.scope.scope]] to a declaration of F,
281
+ ignoring trailing *requires-clause*s, then G *overrides*[^9]
282
+
283
+ F. For convenience we say that any virtual function overrides itself. A
284
+ virtual member function V of a class object S is a *final overrider*
285
+ unless the most derived class [[intro.object]] of which S is a base
286
+ class subobject (if any) has another member function that overrides V.
287
+ In a derived class, if a virtual member function of a base class
288
+ subobject has more than one final overrider the program is ill-formed.
 
289
 
290
  [*Example 1*:
291
 
292
  ``` cpp
293
  struct A {
 
313
 
314
  ``` cpp
315
  struct A { virtual void f(); };
316
  struct B : A { };
317
  struct C : A { void f(); };
318
+ struct D : B, C { }; // OK, A::f and C::f are the final overriders
319
  // for the B and C subobjects, respectively
320
  ```
321
 
322
  — *end example*]
323
 
 
387
  [[dcl.decl]].
388
 
389
  [*Example 5*:
390
 
391
  ``` cpp
392
+ template<typename T>
393
  struct A {
394
  virtual void f() requires true; // error: virtual function cannot be constrained[temp.constr.decl]
395
  };
396
  ```
397
 
398
  — *end example*]
399
 
400
+ The *ref-qualifier*, or lack thereof, of an overriding function shall be
401
+ the same as that of the overridden function.
 
402
 
403
  The return type of an overriding function shall be either identical to
404
  the return type of the overridden function or *covariant* with the
405
  classes of the functions. If a function `D::f` overrides a function
406
  `B::f`, the return types of the functions are covariant if they satisfy
 
417
  as or less cv-qualification than the class type in the return type of
418
  `B::f`.
419
 
420
  If the class type in the covariant return type of `D::f` differs from
421
  that of `B::f`, the class type in the return type of `D::f` shall be
422
+ complete at the locus [[basic.scope.pdecl]] of the overriding
423
+ declaration or shall be the class type `D`. When the overriding function
424
+ is called as the final overrider of the overridden function, its result
425
+ is converted to the type returned by the (statically chosen) overridden
426
+ function [[expr.call]].
427
 
428
  [*Example 6*:
429
 
430
  ``` cpp
431
  class B { };
 
446
  class A;
447
  struct Derived : public Base {
448
  void vf1(); // virtual and overrides Base::vf1()
449
  void vf2(int); // not virtual, hides Base::vf2()
450
  char vf3(); // error: invalid difference in return type only
451
+ D* vf4(); // OK, returns pointer to derived class
452
  A* vf5(); // error: returns pointer to incomplete class
453
  void f();
454
  };
455
 
456
  void g() {
 
479
 
480
  [*Note 4*: The `virtual` specifier implies membership, so a virtual
481
  function cannot be a non-member [[dcl.fct.spec]] function. Nor can a
482
  virtual function be a static member, since a virtual function call
483
  relies on a specific object for determining which function to invoke. A
484
+ virtual function declared in one class can be declared a friend
485
+ [[class.friend]] in another class. — *end note*]
486
 
487
  A virtual function declared in a class shall be defined, or declared
488
  pure [[class.abstract]] in that class, or both; no diagnostic is
489
  required [[basic.def.odr]].
490
 
 
593
  Here, the function call in `D::f` really does call `B::f` and not
594
  `D::f`.
595
 
596
  — *end example*]
597
 
598
+ A deleted function [[dcl.fct.def]] shall not override a function that is
599
+ not deleted. Likewise, a function that is not deleted shall not override
600
+ a deleted function.
 
601
 
602
  A `consteval` virtual function shall not override a virtual function
603
  that is not `consteval`. A `consteval` virtual function shall not be
604
  overridden by a virtual function that is not `consteval`.
605
 
 
621
  A class is an *abstract class* if it has at least one pure virtual
622
  function.
623
 
624
  [*Note 3*: An abstract class can be used only as a base class of some
625
  other class; no objects of an abstract class can be created except as
626
+ subobjects of a class derived from it
627
+ [[basic.def]], [[class.mem]]. — *end note*]
628
 
629
  A pure virtual function need be defined only if called with, or as if
630
  with [[class.dtor]], the *qualified-id* syntax [[expr.prim.id.qual]].
631
 
632
  [*Example 1*:
 
660
 
661
  [*Note 5*: An abstract class type cannot be used as a parameter or
662
  return type of a function being defined [[dcl.fct]] or called
663
  [[expr.call]], except as specified in [[dcl.type.simple]]. Further, an
664
  abstract class type cannot be used as the type of an explicit type
665
+ conversion
666
+ [[expr.static.cast]], [[expr.reinterpret.cast]], [[expr.const.cast]],
667
+ because the resulting prvalue would be of abstract class type
668
+ [[basic.lval]]. However, pointers and references to abstract class types
669
+ can appear in such contexts. — *end note*]
670
 
671
+ A class is abstract if it has at least one pure virtual function for
672
+ which the final overrider is pure virtual.
673
 
674
  [*Example 3*:
675
 
676
  ``` cpp
677
  class ab_circle : public shape {
 
698
  `circle::draw()` must be provided.
699
 
700
  — *end example*]
701
 
702
  [*Note 6*: An abstract class can be derived from a class that is not
703
+ abstract, and a pure virtual function can override a virtual function
704
  which is not pure. — *end note*]
705
 
706
  Member functions can be called from a constructor (or destructor) of an
707
  abstract class; the effect of making a virtual call [[class.virtual]] to
708
  a pure virtual function directly or indirectly for the object being