- tmp/tmpswnk6ron/{from.md → to.md} +144 -366
tmp/tmpswnk6ron/{from.md → to.md}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
| 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
|
|
@@ -15,63 +15,63 @@ base-specifier-list:
|
|
| 15 |
```
|
| 16 |
|
| 17 |
``` bnf
|
| 18 |
base-specifier:
|
| 19 |
attribute-specifier-seqₒₚₜ class-or-decltype
|
| 20 |
-
attribute-specifier-seqₒₚₜ
|
| 21 |
-
attribute-specifier-seqₒₚₜ access-specifier
|
| 22 |
```
|
| 23 |
|
| 24 |
``` bnf
|
| 25 |
class-or-decltype:
|
| 26 |
-
nested-name-specifierₒₚₜ
|
| 27 |
-
nested-name-specifier
|
| 28 |
decltype-specifier
|
| 29 |
```
|
| 30 |
|
| 31 |
``` bnf
|
| 32 |
access-specifier:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
```
|
| 37 |
|
| 38 |
The optional *attribute-specifier-seq* appertains to the
|
| 39 |
*base-specifier*.
|
| 40 |
|
| 41 |
-
A *class-or-decltype* shall denote a
|
| 42 |
-
incompletely defined class
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
[*Note 1*: See
|
| 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
|
| 63 |
|
| 64 |
-
[*Note 2*: The scope resolution operator `::`
|
| 65 |
-
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
|
| 71 |
-
|
| 72 |
-
[[dcl.init.ref]]
|
| 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*:
|
|
@@ -97,41 +97,41 @@ struct Derived2 : Derived {
|
|
| 97 |
Here, an object of class `Derived2` will have a subobject of class
|
| 98 |
`Derived` which in turn will have a subobject of class `Base`.
|
| 99 |
|
| 100 |
— *end example*]
|
| 101 |
|
| 102 |
-
A *base-specifier* followed by an ellipsis is a pack expansion
|
| 103 |
-
[[temp.variadic]]
|
| 104 |
|
| 105 |
The order in which the base class subobjects are allocated in the most
|
| 106 |
-
derived object
|
| 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”
|
| 111 |
-
representation in memory. A DAG of subobjects
|
| 112 |
-
“subobject lattice”.
|
| 113 |
|
| 114 |
-
<a id="fig:dag"></a>
|
| 115 |
|
| 116 |
-
![Directed acyclic graph \[fig: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
|
| 124 |
different from the layout of a most derived object of the same type. A
|
| 125 |
-
base class subobject might have a polymorphic behavior
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 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 |
|
| 136 |
[*Note 1*: The use of more than one direct base class is often called
|
| 137 |
multiple inheritance. — *end note*]
|
|
@@ -146,13 +146,13 @@ class D : public A, public B, public C { ... };
|
|
| 146 |
```
|
| 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]]
|
| 153 |
-
[[class.mem]],
|
| 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
|
|
@@ -164,11 +164,11 @@ can be unambiguously referred to. — *end note*]
|
|
| 164 |
|
| 165 |
[*Example 2*:
|
| 166 |
|
| 167 |
``` cpp
|
| 168 |
class X { ... };
|
| 169 |
-
class Y : public X, public X { ... }; //
|
| 170 |
```
|
| 171 |
|
| 172 |
``` cpp
|
| 173 |
class L { public: int next; ... };
|
| 174 |
class A : public L { ... };
|
|
@@ -181,38 +181,38 @@ class D : public A, public L { void f(); ... }; // well-formed
|
|
| 181 |
|
| 182 |
A base class specifier that does not contain the keyword `virtual`
|
| 183 |
specifies a *non-virtual base class*. A base class specifier that
|
| 184 |
contains the keyword `virtual` specifies a *virtual base class*. For
|
| 185 |
each distinct occurrence of a non-virtual base class in the class
|
| 186 |
-
lattice of the most derived class, the most derived object
|
| 187 |
-
[[intro.object]]
|
| 188 |
subobject of that type. For each distinct base class that is specified
|
| 189 |
virtual, the most derived object shall contain a single base class
|
| 190 |
subobject of that type.
|
| 191 |
|
| 192 |
[*Note 4*:
|
| 193 |
|
| 194 |
For an object of class type `C`, each distinct occurrence of a
|
| 195 |
(non-virtual) base class `L` in the class lattice of `C` corresponds
|
| 196 |
one-to-one with a distinct `L` subobject within the object of type `C`.
|
| 197 |
Given the class `C` defined above, an object of class `C` will have two
|
| 198 |
-
subobjects of class `L` as shown in Figure
|
| 199 |
|
| 200 |
-
<a id="fig:nonvirt"></a>
|
| 201 |
|
| 202 |
-
![Non-virtual base \[fig: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 |
|
| 212 |
Without the `A::` or `B::` qualifiers, the definition of `C::f` above
|
| 213 |
-
would be ill-formed because of ambiguity
|
| 214 |
|
| 215 |
— *end note*]
|
| 216 |
|
| 217 |
[*Note 5*:
|
| 218 |
|
|
@@ -223,19 +223,19 @@ class V { ... };
|
|
| 223 |
class A : virtual public V { ... };
|
| 224 |
class B : virtual public V { ... };
|
| 225 |
class C : public A, public B { ... };
|
| 226 |
```
|
| 227 |
|
| 228 |
-
<a id="fig:virt"></a>
|
| 229 |
|
| 230 |
-
![Virtual base \[fig:virt\]](images/figvirt.svg)
|
| 231 |
|
| 232 |
For an object `c` of class type `C`, a single subobject of type `V` is
|
| 233 |
shared by every base class subobject of `c` that has a `virtual` base
|
| 234 |
class of type `V`. Given the class `C` defined above, an object of class
|
| 235 |
-
`C` will have one subobject of class `V`, as shown in Figure
|
| 236 |
-
[[fig:virt]].
|
| 237 |
|
| 238 |
— *end note*]
|
| 239 |
|
| 240 |
[*Note 6*:
|
| 241 |
|
|
@@ -254,280 +254,43 @@ For an object of class `AA`, all `virtual` occurrences of base class `B`
|
|
| 254 |
in the class lattice of `AA` correspond to a single `B` subobject within
|
| 255 |
the object of type `AA`, and every other occurrence of a (non-virtual)
|
| 256 |
base class `B` in the class lattice of `AA` corresponds one-to-one with
|
| 257 |
a distinct `B` subobject within the object of type `AA`. Given the class
|
| 258 |
`AA` defined above, class `AA` has two subobjects of class `B`: `Z`’s
|
| 259 |
-
`B` and the virtual `B` shared by `X` and `Y`, as shown in Figure
|
| 260 |
-
[[fig:virtnonvirt]].
|
| 261 |
|
| 262 |
-
<a id="fig:virtnonvirt"></a>
|
| 263 |
|
| 264 |
-
![Virtual and non-virtual base \[fig:virtnonvirt\]](images/figvirtnonvirt.svg)
|
| 265 |
|
| 266 |
— *end note*]
|
| 267 |
|
| 268 |
-
##
|
| 269 |
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
*id-expression*, name lookup begins in the class scope of `this`; for a
|
| 274 |
-
*qualified-id*, name lookup begins in the scope of the
|
| 275 |
-
*nested-name-specifier*. Name lookup takes place before access control (
|
| 276 |
-
[[basic.lookup]], Clause [[class.access]]).
|
| 277 |
-
|
| 278 |
-
The following steps define the result of name lookup for a member name
|
| 279 |
-
`f` in a class scope `C`.
|
| 280 |
-
|
| 281 |
-
The *lookup set* for `f` in `C`, called S(f,C), consists of two
|
| 282 |
-
component sets: the *declaration set*, a set of members named `f`; and
|
| 283 |
-
the *subobject set*, a set of subobjects where declarations of these
|
| 284 |
-
members (possibly including *using-declaration*s) were found. In the
|
| 285 |
-
declaration set, *using-declaration*s are replaced by the set of
|
| 286 |
-
designated members that are not hidden or overridden by members of the
|
| 287 |
-
derived class ([[namespace.udecl]]), and type declarations (including
|
| 288 |
-
injected-class-names) are replaced by the types they designate. S(f,C)
|
| 289 |
-
is calculated as follows:
|
| 290 |
-
|
| 291 |
-
If `C` contains a declaration of the name `f`, the declaration set
|
| 292 |
-
contains every declaration of `f` declared in `C` that satisfies the
|
| 293 |
-
requirements of the language construct in which the lookup occurs.
|
| 294 |
-
|
| 295 |
-
[*Note 1*: Looking up a name in an *elaborated-type-specifier* (
|
| 296 |
-
[[basic.lookup.elab]]) or *base-specifier* (Clause [[class.derived]]),
|
| 297 |
-
for instance, ignores all non-type declarations, while looking up a name
|
| 298 |
-
in a *nested-name-specifier* ([[basic.lookup.qual]]) ignores function,
|
| 299 |
-
variable, and enumerator declarations. As another example, looking up a
|
| 300 |
-
name in a *using-declaration* ([[namespace.udecl]]) includes the
|
| 301 |
-
declaration of a class or enumeration that would ordinarily be hidden by
|
| 302 |
-
another declaration of that name in the same scope. — *end note*]
|
| 303 |
-
|
| 304 |
-
If the resulting declaration set is not empty, the subobject set
|
| 305 |
-
contains `C` itself, and calculation is complete.
|
| 306 |
-
|
| 307 |
-
Otherwise (i.e., `C` does not contain a declaration of `f` or the
|
| 308 |
-
resulting declaration set is empty), S(f,C) is initially empty. If `C`
|
| 309 |
-
has base classes, calculate the lookup set for `f` in each direct base
|
| 310 |
-
class subobject Bᵢ, and merge each such lookup set S(f,Bᵢ) in turn into
|
| 311 |
-
S(f,C).
|
| 312 |
-
|
| 313 |
-
The following steps define the result of merging lookup set S(f,Bᵢ) into
|
| 314 |
-
the intermediate S(f,C):
|
| 315 |
-
|
| 316 |
-
- If each of the subobject members of S(f,Bᵢ) is a base class subobject
|
| 317 |
-
of at least one of the subobject members of S(f,C), or if S(f,Bᵢ) is
|
| 318 |
-
empty, S(f,C) is unchanged and the merge is complete. Conversely, if
|
| 319 |
-
each of the subobject members of S(f,C) is a base class subobject of
|
| 320 |
-
at least one of the subobject members of S(f,Bᵢ), or if S(f,C) is
|
| 321 |
-
empty, the new S(f,C) is a copy of S(f,Bᵢ).
|
| 322 |
-
- Otherwise, if the declaration sets of S(f,Bᵢ) and S(f,C) differ, the
|
| 323 |
-
merge is ambiguous: the new S(f,C) is a lookup set with an invalid
|
| 324 |
-
declaration set and the union of the subobject sets. In subsequent
|
| 325 |
-
merges, an invalid declaration set is considered different from any
|
| 326 |
-
other.
|
| 327 |
-
- Otherwise, the new S(f,C) is a lookup set with the shared set of
|
| 328 |
-
declarations and the union of the subobject sets.
|
| 329 |
-
|
| 330 |
-
The result of name lookup for `f` in `C` is the declaration set of
|
| 331 |
-
S(f,C). If it is an invalid set, the program is ill-formed.
|
| 332 |
-
|
| 333 |
-
[*Example 1*:
|
| 334 |
-
|
| 335 |
-
``` cpp
|
| 336 |
-
struct A { int x; }; // S(x,A) = { { A::x }, { A } }
|
| 337 |
-
struct B { float x; }; // S(x,B) = { { B::x }, { B } }
|
| 338 |
-
struct C: public A, public B { }; // S(x,C) = { invalid, { A in C, B in C } }
|
| 339 |
-
struct D: public virtual C { }; // S(x,D) = S(x,C)
|
| 340 |
-
struct E: public virtual C { char x; }; // S(x,E) = { { E::x }, { E } }
|
| 341 |
-
struct F: public D, public E { }; // S(x,F) = S(x,E)
|
| 342 |
-
int main() {
|
| 343 |
-
F f;
|
| 344 |
-
f.x = 0; // OK, lookup finds E::x
|
| 345 |
-
}
|
| 346 |
-
```
|
| 347 |
-
|
| 348 |
-
S(x,F) is unambiguous because the `A` and `B` base class subobjects of
|
| 349 |
-
`D` are also base class subobjects of `E`, so S(x,D) is discarded in the
|
| 350 |
-
first merge step.
|
| 351 |
-
|
| 352 |
-
— *end example*]
|
| 353 |
-
|
| 354 |
-
If the name of an overloaded function is unambiguously found, overload
|
| 355 |
-
resolution ([[over.match]]) also takes place before access control.
|
| 356 |
-
Ambiguities can often be resolved by qualifying a name with its class
|
| 357 |
-
name.
|
| 358 |
-
|
| 359 |
-
[*Example 2*:
|
| 360 |
-
|
| 361 |
-
``` cpp
|
| 362 |
-
struct A {
|
| 363 |
-
int f();
|
| 364 |
-
};
|
| 365 |
-
```
|
| 366 |
-
|
| 367 |
-
``` cpp
|
| 368 |
-
struct B {
|
| 369 |
-
int f();
|
| 370 |
-
};
|
| 371 |
-
```
|
| 372 |
-
|
| 373 |
-
``` cpp
|
| 374 |
-
struct C : A, B {
|
| 375 |
-
int f() { return A::f() + B::f(); }
|
| 376 |
-
};
|
| 377 |
-
```
|
| 378 |
-
|
| 379 |
-
— *end example*]
|
| 380 |
-
|
| 381 |
-
[*Note 2*: A static member, a nested type or an enumerator defined in a
|
| 382 |
-
base class `T` can unambiguously be found even if an object has more
|
| 383 |
-
than one base class subobject of type `T`. Two base class subobjects
|
| 384 |
-
share the non-static member subobjects of their common virtual base
|
| 385 |
-
classes. — *end note*]
|
| 386 |
-
|
| 387 |
-
[*Example 3*:
|
| 388 |
-
|
| 389 |
-
``` cpp
|
| 390 |
-
struct V {
|
| 391 |
-
int v;
|
| 392 |
-
};
|
| 393 |
-
struct A {
|
| 394 |
-
int a;
|
| 395 |
-
static int s;
|
| 396 |
-
enum { e };
|
| 397 |
-
};
|
| 398 |
-
struct B : A, virtual V { };
|
| 399 |
-
struct C : A, virtual V { };
|
| 400 |
-
struct D : B, C { };
|
| 401 |
-
|
| 402 |
-
void f(D* pd) {
|
| 403 |
-
pd->v++; // OK: only one v (virtual)
|
| 404 |
-
pd->s++; // OK: only one s (static)
|
| 405 |
-
int i = pd->e; // OK: only one e (enumerator)
|
| 406 |
-
pd->a++; // error, ambiguous: two a{s} in D
|
| 407 |
-
}
|
| 408 |
-
```
|
| 409 |
-
|
| 410 |
-
— *end example*]
|
| 411 |
-
|
| 412 |
-
[*Note 3*: When virtual base classes are used, a hidden declaration
|
| 413 |
-
can be reached along a path through the subobject lattice that does not
|
| 414 |
-
pass through the hiding declaration. This is not an ambiguity. The
|
| 415 |
-
identical use with non-virtual base classes is an ambiguity; in that
|
| 416 |
-
case there is no unique instance of the name that hides all the
|
| 417 |
-
others. — *end note*]
|
| 418 |
-
|
| 419 |
-
[*Example 4*:
|
| 420 |
-
|
| 421 |
-
``` cpp
|
| 422 |
-
struct V { int f(); int x; };
|
| 423 |
-
struct W { int g(); int y; };
|
| 424 |
-
struct B : virtual V, W {
|
| 425 |
-
int f(); int x;
|
| 426 |
-
int g(); int y;
|
| 427 |
-
};
|
| 428 |
-
struct C : virtual V, W { };
|
| 429 |
-
|
| 430 |
-
struct D : B, C { void glorp(); };
|
| 431 |
-
```
|
| 432 |
-
|
| 433 |
-
<a id="fig:name"></a>
|
| 434 |
-
|
| 435 |
-
![Name lookup \[fig:name\]](images/figname.svg)
|
| 436 |
-
|
| 437 |
-
The names declared in `V` and the left-hand instance of `W` are hidden
|
| 438 |
-
by those in `B`, but the names declared in the right-hand instance of
|
| 439 |
-
`W` are not hidden at all.
|
| 440 |
-
|
| 441 |
-
``` cpp
|
| 442 |
-
void D::glorp() {
|
| 443 |
-
x++; // OK: B::x hides V::x
|
| 444 |
-
f(); // OK: B::f() hides V::f()
|
| 445 |
-
y++; // error: B::y and C's W::y
|
| 446 |
-
g(); // error: B::g() and C's W::g()
|
| 447 |
-
}
|
| 448 |
-
```
|
| 449 |
-
|
| 450 |
-
— *end example*]
|
| 451 |
-
|
| 452 |
-
An explicit or implicit conversion from a pointer to or an expression
|
| 453 |
-
designating an object of a derived class to a pointer or reference to
|
| 454 |
-
one of its base classes shall unambiguously refer to a unique object
|
| 455 |
-
representing the base class.
|
| 456 |
-
|
| 457 |
-
[*Example 5*:
|
| 458 |
-
|
| 459 |
-
``` cpp
|
| 460 |
-
struct V { };
|
| 461 |
-
struct A { };
|
| 462 |
-
struct B : A, virtual V { };
|
| 463 |
-
struct C : A, virtual V { };
|
| 464 |
-
struct D : B, C { };
|
| 465 |
-
|
| 466 |
-
void g() {
|
| 467 |
-
D d;
|
| 468 |
-
B* pb = &d;
|
| 469 |
-
A* pa = &d; // error, ambiguous: C's A or B's A?
|
| 470 |
-
V* pv = &d; // OK: only one V subobject
|
| 471 |
-
}
|
| 472 |
-
```
|
| 473 |
-
|
| 474 |
-
— *end example*]
|
| 475 |
-
|
| 476 |
-
[*Note 4*: Even if the result of name lookup is unambiguous, use of a
|
| 477 |
-
name found in multiple subobjects might still be ambiguous (
|
| 478 |
-
[[conv.mem]], [[expr.ref]], [[class.access.base]]). — *end note*]
|
| 479 |
-
|
| 480 |
-
[*Example 6*:
|
| 481 |
-
|
| 482 |
-
``` cpp
|
| 483 |
-
struct B1 {
|
| 484 |
-
void f();
|
| 485 |
-
static void f(int);
|
| 486 |
-
int i;
|
| 487 |
-
};
|
| 488 |
-
struct B2 {
|
| 489 |
-
void f(double);
|
| 490 |
-
};
|
| 491 |
-
struct I1: B1 { };
|
| 492 |
-
struct I2: B1 { };
|
| 493 |
-
|
| 494 |
-
struct D: I1, I2, B2 {
|
| 495 |
-
using B1::f;
|
| 496 |
-
using B2::f;
|
| 497 |
-
void g() {
|
| 498 |
-
f(); // Ambiguous conversion of this
|
| 499 |
-
f(0); // Unambiguous (static)
|
| 500 |
-
f(0.0); // Unambiguous (only one B2)
|
| 501 |
-
int B1::* mpB1 = &D::i; // Unambiguous
|
| 502 |
-
int D::* mpD = &D::i; // Ambiguous conversion
|
| 503 |
-
}
|
| 504 |
-
};
|
| 505 |
-
```
|
| 506 |
-
|
| 507 |
-
— *end example*]
|
| 508 |
-
|
| 509 |
-
## Virtual functions <a id="class.virtual">[[class.virtual]]</a>
|
| 510 |
|
| 511 |
[*Note 1*: Virtual functions support dynamic binding and
|
| 512 |
object-oriented programming. — *end note*]
|
| 513 |
|
| 514 |
A class that declares or inherits a virtual function is called a
|
| 515 |
-
*polymorphic class*.
|
| 516 |
|
| 517 |
If a virtual member function `vf` is declared in a class `Base` and in a
|
| 518 |
class `Derived`, derived directly or indirectly from `Base`, a member
|
| 519 |
-
function `vf` with the same name, parameter-type-list
|
| 520 |
cv-qualification, and ref-qualifier (or absence of same) as `Base::vf`
|
| 521 |
-
is declared, then `Derived::vf`
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
class
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
|
| 530 |
[*Example 1*:
|
| 531 |
|
| 532 |
``` cpp
|
| 533 |
struct A {
|
|
@@ -621,10 +384,23 @@ struct D : B {
|
|
| 621 |
};
|
| 622 |
```
|
| 623 |
|
| 624 |
— *end example*]
|
| 625 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 626 |
Even though destructors are not inherited, a destructor in a derived
|
| 627 |
class overrides a base class destructor declared virtual; see
|
| 628 |
[[class.dtor]] and [[class.free]].
|
| 629 |
|
| 630 |
The return type of an overriding function shall be either identical to
|
|
@@ -632,11 +408,11 @@ the return type of the overridden function or *covariant* with the
|
|
| 632 |
classes of the functions. If a function `D::f` overrides a function
|
| 633 |
`B::f`, the return types of the functions are covariant if they satisfy
|
| 634 |
the following criteria:
|
| 635 |
|
| 636 |
- both are pointers to classes, both are lvalue references to classes,
|
| 637 |
-
or both are rvalue references to classes[^
|
| 638 |
- the class in the return type of `B::f` is the same class as the class
|
| 639 |
in the return type of `D::f`, or is an unambiguous and accessible
|
| 640 |
direct or indirect base class of the class in the return type of
|
| 641 |
`D::f`
|
| 642 |
- both pointers or references have the same cv-qualification and the
|
|
@@ -647,13 +423,13 @@ the following criteria:
|
|
| 647 |
If the class type in the covariant return type of `D::f` differs from
|
| 648 |
that of `B::f`, the class type in the return type of `D::f` shall be
|
| 649 |
complete at the point of declaration of `D::f` or shall be the class
|
| 650 |
type `D`. When the overriding function is called as the final overrider
|
| 651 |
of the overridden function, its result is converted to the type returned
|
| 652 |
-
by the (statically chosen) overridden function
|
| 653 |
|
| 654 |
-
[*Example
|
| 655 |
|
| 656 |
``` cpp
|
| 657 |
class B { };
|
| 658 |
class D : private B { friend class Derived; };
|
| 659 |
struct Base {
|
|
@@ -684,39 +460,39 @@ void g() {
|
|
| 684 |
Base* bp = &d; // standard conversion:
|
| 685 |
// Derived* to Base*
|
| 686 |
bp->vf1(); // calls Derived::vf1()
|
| 687 |
bp->vf2(); // calls Base::vf2()
|
| 688 |
bp->f(); // calls Base::f() (not virtual)
|
| 689 |
-
B* p = bp->vf4(); // calls Derived::
|
| 690 |
// result to B*
|
| 691 |
Derived* dp = &d;
|
| 692 |
-
D* q = dp->vf4(); // calls Derived::
|
| 693 |
// convert the result to B*
|
| 694 |
-
dp->vf2(); //
|
| 695 |
}
|
| 696 |
```
|
| 697 |
|
| 698 |
— *end example*]
|
| 699 |
|
| 700 |
[*Note 3*: The interpretation of the call of a virtual function depends
|
| 701 |
on the type of the object for which it is called (the dynamic type),
|
| 702 |
whereas the interpretation of a call of a non-virtual member function
|
| 703 |
depends only on the type of the pointer or reference denoting that
|
| 704 |
-
object (the static type)
|
| 705 |
|
| 706 |
[*Note 4*: The `virtual` specifier implies membership, so a virtual
|
| 707 |
-
function cannot be a non-member
|
| 708 |
virtual function be a static member, since a virtual function call
|
| 709 |
relies on a specific object for determining which function to invoke. A
|
| 710 |
-
virtual function declared in one class can be declared a
|
| 711 |
-
another class. — *end note*]
|
| 712 |
|
| 713 |
A virtual function declared in a class shall be defined, or declared
|
| 714 |
-
pure
|
| 715 |
-
required
|
| 716 |
|
| 717 |
-
[*Example
|
| 718 |
|
| 719 |
Here are some uses of virtual functions with multiple base classes:
|
| 720 |
|
| 721 |
``` cpp
|
| 722 |
struct A {
|
|
@@ -739,22 +515,22 @@ void foo() {
|
|
| 739 |
// A* ap = &d; // would be ill-formed: ambiguous
|
| 740 |
B1* b1p = &d;
|
| 741 |
A* ap = b1p;
|
| 742 |
D* dp = &d;
|
| 743 |
ap->f(); // calls D::B1::f
|
| 744 |
-
dp->f(); //
|
| 745 |
}
|
| 746 |
```
|
| 747 |
|
| 748 |
In class `D` above there are two occurrences of class `A` and hence two
|
| 749 |
occurrences of the virtual member function `A::f`. The final overrider
|
| 750 |
of `B1::A::f` is `B1::f` and the final overrider of `B2::A::f` is
|
| 751 |
`B2::f`.
|
| 752 |
|
| 753 |
— *end example*]
|
| 754 |
|
| 755 |
-
[*Example
|
| 756 |
|
| 757 |
The following example shows a function that does not have a unique final
|
| 758 |
overrider:
|
| 759 |
|
| 760 |
``` cpp
|
|
@@ -768,26 +544,26 @@ struct VB1 : virtual A { // note virtual derivation
|
|
| 768 |
|
| 769 |
struct VB2 : virtual A {
|
| 770 |
void f();
|
| 771 |
};
|
| 772 |
|
| 773 |
-
struct Error : VB1, VB2 { //
|
| 774 |
};
|
| 775 |
|
| 776 |
struct Okay : VB1, VB2 {
|
| 777 |
void f();
|
| 778 |
};
|
| 779 |
```
|
| 780 |
|
| 781 |
Both `VB1::f` and `VB2::f` override `A::f` but there is no overrider of
|
| 782 |
both of them in class `Error`. This example is therefore ill-formed.
|
| 783 |
-
Class `Okay` is well
|
| 784 |
overrider.
|
| 785 |
|
| 786 |
— *end example*]
|
| 787 |
|
| 788 |
-
[*Example
|
| 789 |
|
| 790 |
The following example uses the well-formed classes from above.
|
| 791 |
|
| 792 |
``` cpp
|
| 793 |
struct VB1a : virtual A { // does not declare f
|
|
@@ -802,14 +578,14 @@ void foe() {
|
|
| 802 |
}
|
| 803 |
```
|
| 804 |
|
| 805 |
— *end example*]
|
| 806 |
|
| 807 |
-
Explicit qualification with the scope operator
|
| 808 |
suppresses the virtual call mechanism.
|
| 809 |
|
| 810 |
-
[*Example
|
| 811 |
|
| 812 |
``` cpp
|
| 813 |
class B { public: virtual void f(); };
|
| 814 |
class D : public B { public: void f(); };
|
| 815 |
|
|
@@ -819,35 +595,44 @@ void D::f() { ... B::f(); }
|
|
| 819 |
Here, the function call in `D::f` really does call `B::f` and not
|
| 820 |
`D::f`.
|
| 821 |
|
| 822 |
— *end example*]
|
| 823 |
|
| 824 |
-
A function with a deleted definition
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
|
| 829 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 830 |
|
| 831 |
[*Note 1*: The abstract class mechanism supports the notion of a
|
| 832 |
general concept, such as a `shape`, of which only more concrete
|
| 833 |
variants, such as `circle` and `square`, can actually be used. An
|
| 834 |
abstract class can also be used to define an interface for which derived
|
| 835 |
classes provide a variety of implementations. — *end note*]
|
| 836 |
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
at least one *pure virtual function*.
|
| 841 |
|
| 842 |
[*Note 2*: Such a function might be inherited: see
|
| 843 |
below. — *end note*]
|
| 844 |
|
| 845 |
-
A
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 849 |
|
| 850 |
[*Example 1*:
|
| 851 |
|
| 852 |
``` cpp
|
| 853 |
class point { ... };
|
|
@@ -861,43 +646,36 @@ public:
|
|
| 861 |
};
|
| 862 |
```
|
| 863 |
|
| 864 |
— *end example*]
|
| 865 |
|
| 866 |
-
[*Note
|
| 867 |
-
*pure-specifier* and a definition — *end note*]
|
| 868 |
|
| 869 |
[*Example 2*:
|
| 870 |
|
| 871 |
``` cpp
|
| 872 |
struct C {
|
| 873 |
-
virtual void f() = 0 { }; //
|
| 874 |
};
|
| 875 |
```
|
| 876 |
|
| 877 |
— *end example*]
|
| 878 |
|
| 879 |
-
An abstract class
|
| 880 |
-
return type
|
| 881 |
-
|
| 882 |
-
|
| 883 |
-
[
|
| 884 |
-
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
shape* p; // OK
|
| 888 |
-
shape f(); // error
|
| 889 |
-
void g(shape); // error
|
| 890 |
-
shape& h(shape&); // OK
|
| 891 |
-
```
|
| 892 |
-
|
| 893 |
-
— *end example*]
|
| 894 |
|
| 895 |
A class is abstract if it contains or inherits at least one pure virtual
|
| 896 |
function for which the final overrider is pure virtual.
|
| 897 |
|
| 898 |
-
[*Example
|
| 899 |
|
| 900 |
``` cpp
|
| 901 |
class ab_circle : public shape {
|
| 902 |
int radius;
|
| 903 |
public:
|
|
@@ -921,15 +699,15 @@ public:
|
|
| 921 |
would make class `circle` non-abstract and a definition of
|
| 922 |
`circle::draw()` must be provided.
|
| 923 |
|
| 924 |
— *end example*]
|
| 925 |
|
| 926 |
-
[*Note
|
| 927 |
abstract, and a pure virtual function may override a virtual function
|
| 928 |
which is not pure. — *end note*]
|
| 929 |
|
| 930 |
Member functions can be called from a constructor (or destructor) of an
|
| 931 |
-
abstract class; the effect of making a virtual call
|
| 932 |
-
|
| 933 |
created (or destroyed) from such a constructor (or destructor) is
|
| 934 |
undefined.
|
| 935 |
|
|
|
|
| 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
|
|
|
|
| 15 |
```
|
| 16 |
|
| 17 |
``` bnf
|
| 18 |
base-specifier:
|
| 19 |
attribute-specifier-seqₒₚₜ class-or-decltype
|
| 20 |
+
attribute-specifier-seqₒₚₜ virtual access-specifierₒₚₜ class-or-decltype
|
| 21 |
+
attribute-specifier-seqₒₚₜ access-specifier virtualₒₚₜ class-or-decltype
|
| 22 |
```
|
| 23 |
|
| 24 |
``` bnf
|
| 25 |
class-or-decltype:
|
| 26 |
+
nested-name-specifierₒₚₜ type-name
|
| 27 |
+
nested-name-specifier template simple-template-id
|
| 28 |
decltype-specifier
|
| 29 |
```
|
| 30 |
|
| 31 |
``` bnf
|
| 32 |
access-specifier:
|
| 33 |
+
private
|
| 34 |
+
protected
|
| 35 |
+
public
|
| 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*:
|
|
|
|
| 97 |
Here, an object of class `Derived2` will have a subobject of class
|
| 98 |
`Derived` which in turn will have a subobject of class `Base`.
|
| 99 |
|
| 100 |
— *end example*]
|
| 101 |
|
| 102 |
+
A *base-specifier* followed by an ellipsis is a pack expansion
|
| 103 |
+
[[temp.variadic]].
|
| 104 |
|
| 105 |
The order in which the base class subobjects are allocated in the most
|
| 106 |
+
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 |
|
| 136 |
[*Note 1*: The use of more than one direct base class is often called
|
| 137 |
multiple inheritance. — *end note*]
|
|
|
|
| 146 |
```
|
| 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
|
|
|
|
| 164 |
|
| 165 |
[*Example 2*:
|
| 166 |
|
| 167 |
``` cpp
|
| 168 |
class X { ... };
|
| 169 |
+
class Y : public X, public X { ... }; // error
|
| 170 |
```
|
| 171 |
|
| 172 |
``` cpp
|
| 173 |
class L { public: int next; ... };
|
| 174 |
class A : public L { ... };
|
|
|
|
| 181 |
|
| 182 |
A base class specifier that does not contain the keyword `virtual`
|
| 183 |
specifies a *non-virtual base class*. A base class specifier that
|
| 184 |
contains the keyword `virtual` specifies a *virtual base class*. For
|
| 185 |
each distinct occurrence of a non-virtual base class in the class
|
| 186 |
+
lattice of the most derived class, the most derived object
|
| 187 |
+
[[intro.object]] shall contain a corresponding distinct base class
|
| 188 |
subobject of that type. For each distinct base class that is specified
|
| 189 |
virtual, the most derived object shall contain a single base class
|
| 190 |
subobject of that type.
|
| 191 |
|
| 192 |
[*Note 4*:
|
| 193 |
|
| 194 |
For an object of class type `C`, each distinct occurrence of a
|
| 195 |
(non-virtual) base class `L` in the class lattice of `C` corresponds
|
| 196 |
one-to-one with a distinct `L` subobject within the object of type `C`.
|
| 197 |
Given the class `C` defined above, an object of class `C` will have two
|
| 198 |
+
subobjects of class `L` as shown in Figure [[fig:class.nonvirt]].
|
| 199 |
|
| 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 |
|
| 212 |
Without the `A::` or `B::` qualifiers, the definition of `C::f` above
|
| 213 |
+
would be ill-formed because of ambiguity [[class.member.lookup]].
|
| 214 |
|
| 215 |
— *end note*]
|
| 216 |
|
| 217 |
[*Note 5*:
|
| 218 |
|
|
|
|
| 223 |
class A : virtual public V { ... };
|
| 224 |
class B : virtual public V { ... };
|
| 225 |
class C : public A, public B { ... };
|
| 226 |
```
|
| 227 |
|
| 228 |
+
<a id="fig:class.virt"></a>
|
| 229 |
|
| 230 |
+
![Virtual base \[fig:class.virt\]](images/figvirt.svg)
|
| 231 |
|
| 232 |
For an object `c` of class type `C`, a single subobject of type `V` is
|
| 233 |
shared by every base class subobject of `c` that has a `virtual` base
|
| 234 |
class of type `V`. Given the class `C` defined above, an object of class
|
| 235 |
+
`C` will have one subobject of class `V`, as shown in Figure
|
| 236 |
+
[[fig:class.virt]].
|
| 237 |
|
| 238 |
— *end note*]
|
| 239 |
|
| 240 |
[*Note 6*:
|
| 241 |
|
|
|
|
| 254 |
in the class lattice of `AA` correspond to a single `B` subobject within
|
| 255 |
the object of type `AA`, and every other occurrence of a (non-virtual)
|
| 256 |
base class `B` in the class lattice of `AA` corresponds one-to-one with
|
| 257 |
a distinct `B` subobject within the object of type `AA`. Given the class
|
| 258 |
`AA` defined above, class `AA` has two subobjects of class `B`: `Z`’s
|
| 259 |
+
`B` and the virtual `B` shared by `X` and `Y`, as shown in Figure
|
| 260 |
+
[[fig:class.virtnonvirt]].
|
| 261 |
|
| 262 |
+
<a id="fig:class.virtnonvirt"></a>
|
| 263 |
|
| 264 |
+
![Virtual and non-virtual base \[fig:class.virtnonvirt\]](images/figvirtnonvirt.svg)
|
| 265 |
|
| 266 |
— *end note*]
|
| 267 |
|
| 268 |
+
### Virtual functions <a id="class.virtual">[[class.virtual]]</a>
|
| 269 |
|
| 270 |
+
A non-static member function is a *virtual function* if it is first
|
| 271 |
+
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 {
|
|
|
|
| 384 |
};
|
| 385 |
```
|
| 386 |
|
| 387 |
— *end example*]
|
| 388 |
|
| 389 |
+
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
|
|
|
|
| 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
|
| 410 |
the following criteria:
|
| 411 |
|
| 412 |
- both are pointers to classes, both are lvalue references to classes,
|
| 413 |
+
or both are rvalue references to classes[^10]
|
| 414 |
- the class in the return type of `B::f` is the same class as the class
|
| 415 |
in the return type of `D::f`, or is an unambiguous and accessible
|
| 416 |
direct or indirect base class of the class in the return type of
|
| 417 |
`D::f`
|
| 418 |
- both pointers or references have the same cv-qualification and the
|
|
|
|
| 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 { };
|
| 434 |
class D : private B { friend class Derived; };
|
| 435 |
struct Base {
|
|
|
|
| 460 |
Base* bp = &d; // standard conversion:
|
| 461 |
// Derived* to Base*
|
| 462 |
bp->vf1(); // calls Derived::vf1()
|
| 463 |
bp->vf2(); // calls Base::vf2()
|
| 464 |
bp->f(); // calls Base::f() (not virtual)
|
| 465 |
+
B* p = bp->vf4(); // calls Derived::vf4() and converts the
|
| 466 |
// result to B*
|
| 467 |
Derived* dp = &d;
|
| 468 |
+
D* q = dp->vf4(); // calls Derived::vf4() and does not
|
| 469 |
// convert the result to B*
|
| 470 |
+
dp->vf2(); // error: argument mismatch
|
| 471 |
}
|
| 472 |
```
|
| 473 |
|
| 474 |
— *end example*]
|
| 475 |
|
| 476 |
[*Note 3*: The interpretation of the call of a virtual function depends
|
| 477 |
on the type of the object for which it is called (the dynamic type),
|
| 478 |
whereas the interpretation of a call of a non-virtual member function
|
| 479 |
depends only on the type of the pointer or reference denoting that
|
| 480 |
+
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 |
|
| 493 |
+
[*Example 7*:
|
| 494 |
|
| 495 |
Here are some uses of virtual functions with multiple base classes:
|
| 496 |
|
| 497 |
``` cpp
|
| 498 |
struct A {
|
|
|
|
| 515 |
// A* ap = &d; // would be ill-formed: ambiguous
|
| 516 |
B1* b1p = &d;
|
| 517 |
A* ap = b1p;
|
| 518 |
D* dp = &d;
|
| 519 |
ap->f(); // calls D::B1::f
|
| 520 |
+
dp->f(); // error: ambiguous
|
| 521 |
}
|
| 522 |
```
|
| 523 |
|
| 524 |
In class `D` above there are two occurrences of class `A` and hence two
|
| 525 |
occurrences of the virtual member function `A::f`. The final overrider
|
| 526 |
of `B1::A::f` is `B1::f` and the final overrider of `B2::A::f` is
|
| 527 |
`B2::f`.
|
| 528 |
|
| 529 |
— *end example*]
|
| 530 |
|
| 531 |
+
[*Example 8*:
|
| 532 |
|
| 533 |
The following example shows a function that does not have a unique final
|
| 534 |
overrider:
|
| 535 |
|
| 536 |
``` cpp
|
|
|
|
| 544 |
|
| 545 |
struct VB2 : virtual A {
|
| 546 |
void f();
|
| 547 |
};
|
| 548 |
|
| 549 |
+
struct Error : VB1, VB2 { // error
|
| 550 |
};
|
| 551 |
|
| 552 |
struct Okay : VB1, VB2 {
|
| 553 |
void f();
|
| 554 |
};
|
| 555 |
```
|
| 556 |
|
| 557 |
Both `VB1::f` and `VB2::f` override `A::f` but there is no overrider of
|
| 558 |
both of them in class `Error`. This example is therefore ill-formed.
|
| 559 |
+
Class `Okay` is well-formed, however, because `Okay::f` is a final
|
| 560 |
overrider.
|
| 561 |
|
| 562 |
— *end example*]
|
| 563 |
|
| 564 |
+
[*Example 9*:
|
| 565 |
|
| 566 |
The following example uses the well-formed classes from above.
|
| 567 |
|
| 568 |
``` cpp
|
| 569 |
struct VB1a : virtual A { // does not declare f
|
|
|
|
| 578 |
}
|
| 579 |
```
|
| 580 |
|
| 581 |
— *end example*]
|
| 582 |
|
| 583 |
+
Explicit qualification with the scope operator [[expr.prim.id.qual]]
|
| 584 |
suppresses the virtual call mechanism.
|
| 585 |
|
| 586 |
+
[*Example 10*:
|
| 587 |
|
| 588 |
``` cpp
|
| 589 |
class B { public: virtual void f(); };
|
| 590 |
class D : public B { public: void f(); };
|
| 591 |
|
|
|
|
| 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 |
+
|
| 609 |
+
### Abstract classes <a id="class.abstract">[[class.abstract]]</a>
|
| 610 |
|
| 611 |
[*Note 1*: The abstract class mechanism supports the notion of a
|
| 612 |
general concept, such as a `shape`, of which only more concrete
|
| 613 |
variants, such as `circle` and `square`, can actually be used. An
|
| 614 |
abstract class can also be used to define an interface for which derived
|
| 615 |
classes provide a variety of implementations. — *end note*]
|
| 616 |
|
| 617 |
+
A virtual function is specified as a *pure virtual function* by using a
|
| 618 |
+
*pure-specifier* [[class.mem]] in the function declaration in the class
|
| 619 |
+
definition.
|
|
|
|
| 620 |
|
| 621 |
[*Note 2*: Such a function might be inherited: see
|
| 622 |
below. — *end note*]
|
| 623 |
|
| 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*:
|
| 636 |
|
| 637 |
``` cpp
|
| 638 |
class point { ... };
|
|
|
|
| 646 |
};
|
| 647 |
```
|
| 648 |
|
| 649 |
— *end example*]
|
| 650 |
|
| 651 |
+
[*Note 4*: A function declaration cannot provide both a
|
| 652 |
+
*pure-specifier* and a definition. — *end note*]
|
| 653 |
|
| 654 |
[*Example 2*:
|
| 655 |
|
| 656 |
``` cpp
|
| 657 |
struct C {
|
| 658 |
+
virtual void f() = 0 { }; // error
|
| 659 |
};
|
| 660 |
```
|
| 661 |
|
| 662 |
— *end example*]
|
| 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 {
|
| 680 |
int radius;
|
| 681 |
public:
|
|
|
|
| 699 |
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
|
| 711 |
created (or destroyed) from such a constructor (or destructor) is
|
| 712 |
undefined.
|
| 713 |
|