- tmp/tmpzrojk6uw/{from.md → to.md} +280 -152
tmp/tmpzrojk6uw/{from.md → to.md}
RENAMED
|
@@ -14,65 +14,70 @@ base-specifier-list:
|
|
| 14 |
base-specifier-list ',' base-specifier '...'ₒₚₜ
|
| 15 |
```
|
| 16 |
|
| 17 |
``` bnf
|
| 18 |
base-specifier:
|
| 19 |
-
attribute-specifier-seqₒₚₜ
|
| 20 |
-
attribute-specifier-seqₒₚₜ 'virtual' access-specifierₒₚₜ
|
| 21 |
-
attribute-specifier-seqₒₚₜ access-specifier 'virtual'ₒₚₜ
|
| 22 |
```
|
| 23 |
|
| 24 |
``` bnf
|
| 25 |
class-or-decltype:
|
| 26 |
nested-name-specifierₒₚₜ class-name
|
|
|
|
| 27 |
decltype-specifier
|
| 28 |
```
|
| 29 |
|
| 30 |
-
``` bnf
|
| 31 |
-
base-type-specifier:
|
| 32 |
-
class-or-decltype
|
| 33 |
-
```
|
| 34 |
-
|
| 35 |
``` bnf
|
| 36 |
access-specifier:
|
| 37 |
'private'
|
| 38 |
'protected'
|
| 39 |
'public'
|
| 40 |
```
|
| 41 |
|
| 42 |
The optional *attribute-specifier-seq* appertains to the
|
| 43 |
*base-specifier*.
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
[[basic.scope.hiding]]). If the name
|
| 50 |
-
program is ill-formed. A class `B` is a
|
| 51 |
-
|
| 52 |
-
base classes. A class is an *indirect*
|
| 53 |
-
base class
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
derived class,
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
The *base-specifier-list* specifies the type of the *base class
|
| 72 |
subobjects* contained in an object of the derived class type.
|
| 73 |
|
|
|
|
|
|
|
| 74 |
``` cpp
|
| 75 |
struct Base {
|
| 76 |
int a, b, c;
|
| 77 |
};
|
| 78 |
```
|
|
@@ -90,88 +95,109 @@ struct Derived2 : Derived {
|
|
| 90 |
```
|
| 91 |
|
| 92 |
Here, an object of class `Derived2` will have a subobject of class
|
| 93 |
`Derived` which in turn will have a subobject of class `Base`.
|
| 94 |
|
|
|
|
|
|
|
| 95 |
A *base-specifier* followed by an ellipsis is a pack expansion (
|
| 96 |
[[temp.variadic]]).
|
| 97 |
|
| 98 |
The order in which the base class subobjects are allocated in the most
|
| 99 |
-
derived object ([[intro.object]]) is unspecified.
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
<a id="fig:dag"></a>
|
| 105 |
|
| 106 |
![Directed acyclic graph \[fig:dag\]](images/figdag.svg)
|
| 107 |
|
| 108 |
-
|
| 109 |
|
| 110 |
-
Initialization of objects representing base classes can be
|
| 111 |
-
constructors; see [[class.base.init]].
|
| 112 |
|
| 113 |
-
A base class subobject might have a layout ([[basic.stc]])
|
| 114 |
-
from the layout of a most derived object of the same type. A
|
| 115 |
-
subobject might have a polymorphic behavior (
|
| 116 |
-
from the polymorphic behavior of a most
|
| 117 |
-
A base class subobject may be of zero
|
| 118 |
-
two subobjects that have the same
|
| 119 |
-
most derived object must not be
|
| 120 |
-
[[expr.eq]]).
|
| 121 |
|
| 122 |
## Multiple base classes <a id="class.mi">[[class.mi]]</a>
|
| 123 |
|
| 124 |
-
A class can be derived from any number of base classes.
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
``` cpp
|
| 128 |
-
class A {
|
| 129 |
-
class B {
|
| 130 |
-
class C {
|
| 131 |
-
class D : public A, public B, public C {
|
| 132 |
```
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
| 138 |
|
| 139 |
A class shall not be specified as a direct base class of a derived class
|
| 140 |
-
more than once.
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
``` cpp
|
| 148 |
-
class X {
|
| 149 |
-
class Y : public X, public X {
|
| 150 |
```
|
| 151 |
|
| 152 |
``` cpp
|
| 153 |
-
class L { public: int next;
|
| 154 |
-
class A : public L {
|
| 155 |
-
class B : public L {
|
| 156 |
-
class C : public A, public B { void f();
|
| 157 |
-
class D : public A, public L { void f();
|
| 158 |
```
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
| 163 |
each distinct occurrence of a non-virtual base class in the class
|
| 164 |
lattice of the most derived class, the most derived object (
|
| 165 |
[[intro.object]]) shall contain a corresponding distinct base class
|
| 166 |
subobject of that type. For each distinct base class that is specified
|
| 167 |
virtual, the most derived object shall contain a single base class
|
| 168 |
-
subobject of that type.
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
<a id="fig:nonvirt"></a>
|
| 175 |
|
| 176 |
![Non-virtual base \[fig:nonvirt\]](images/fignonvirt.svg)
|
| 177 |
|
|
@@ -184,51 +210,63 @@ void C::f() { A::next = B::next; } // well-formed
|
|
| 184 |
```
|
| 185 |
|
| 186 |
Without the `A::` or `B::` qualifiers, the definition of `C::f` above
|
| 187 |
would be ill-formed because of ambiguity ([[class.member.lookup]]).
|
| 188 |
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
``` cpp
|
| 192 |
-
class V {
|
| 193 |
-
class A : virtual public V {
|
| 194 |
-
class B : virtual public V {
|
| 195 |
-
class C : public A, public B {
|
| 196 |
```
|
| 197 |
|
| 198 |
-
for an object `c` of class type `C`, a single subobject of type `V` is
|
| 199 |
-
shared by every base subobject of `c` that has a `virtual` base class of
|
| 200 |
-
type `V`. Given the class `C` defined above, an object of class `C` will
|
| 201 |
-
have one subobject of class `V`, as shown below.
|
| 202 |
-
|
| 203 |
<a id="fig:virt"></a>
|
| 204 |
|
| 205 |
![Virtual base \[fig:virt\]](images/figvirt.svg)
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
A class can have both virtual and non-virtual base classes of a given
|
| 208 |
type.
|
| 209 |
|
| 210 |
``` cpp
|
| 211 |
-
class B {
|
| 212 |
-
class X : virtual public B {
|
| 213 |
-
class Y : virtual public B {
|
| 214 |
-
class Z : public B {
|
| 215 |
-
class AA : public X, public Y, public Z {
|
| 216 |
```
|
| 217 |
|
| 218 |
For an object of class `AA`, all `virtual` occurrences of base class `B`
|
| 219 |
in the class lattice of `AA` correspond to a single `B` subobject within
|
| 220 |
the object of type `AA`, and every other occurrence of a (non-virtual)
|
| 221 |
base class `B` in the class lattice of `AA` corresponds one-to-one with
|
| 222 |
a distinct `B` subobject within the object of type `AA`. Given the class
|
| 223 |
`AA` defined above, class `AA` has two subobjects of class `B`: `Z`’s
|
| 224 |
-
`B` and the virtual `B` shared by `X` and `Y`, as shown
|
|
|
|
| 225 |
|
| 226 |
<a id="fig:virtnonvirt"></a>
|
| 227 |
|
| 228 |
![Virtual and non-virtual base \[fig:virtnonvirt\]](images/figvirtnonvirt.svg)
|
| 229 |
|
|
|
|
|
|
|
| 230 |
## Member name lookup <a id="class.member.lookup">[[class.member.lookup]]</a>
|
| 231 |
|
| 232 |
Member name lookup determines the meaning of a name (*id-expression*) in
|
| 233 |
a class scope ([[basic.scope.class]]). Name lookup can result in an
|
| 234 |
*ambiguity*, in which case the program is ill-formed. For an
|
|
@@ -251,20 +289,22 @@ injected-class-names) are replaced by the types they designate. S(f,C)
|
|
| 251 |
is calculated as follows:
|
| 252 |
|
| 253 |
If `C` contains a declaration of the name `f`, the declaration set
|
| 254 |
contains every declaration of `f` declared in `C` that satisfies the
|
| 255 |
requirements of the language construct in which the lookup occurs.
|
| 256 |
-
|
|
|
|
| 257 |
[[basic.lookup.elab]]) or *base-specifier* (Clause [[class.derived]]),
|
| 258 |
for instance, ignores all non-type declarations, while looking up a name
|
| 259 |
in a *nested-name-specifier* ([[basic.lookup.qual]]) ignores function,
|
| 260 |
variable, and enumerator declarations. As another example, looking up a
|
| 261 |
name in a *using-declaration* ([[namespace.udecl]]) includes the
|
| 262 |
declaration of a class or enumeration that would ordinarily be hidden by
|
| 263 |
-
another declaration of that name in the same scope.
|
| 264 |
-
|
| 265 |
-
|
|
|
|
| 266 |
|
| 267 |
Otherwise (i.e., `C` does not contain a declaration of `f` or the
|
| 268 |
resulting declaration set is empty), S(f,C) is initially empty. If `C`
|
| 269 |
has base classes, calculate the lookup set for `f` in each direct base
|
| 270 |
class subobject Bᵢ, and merge each such lookup set S(f,Bᵢ) in turn into
|
|
@@ -288,10 +328,12 @@ the intermediate S(f,C):
|
|
| 288 |
declarations and the union of the subobject sets.
|
| 289 |
|
| 290 |
The result of name lookup for `f` in `C` is the declaration set of
|
| 291 |
S(f,C). If it is an invalid set, the program is ill-formed.
|
| 292 |
|
|
|
|
|
|
|
| 293 |
``` cpp
|
| 294 |
struct A { int x; }; // S(x,A) = { { A::x }, { A } }
|
| 295 |
struct B { float x; }; // S(x,B) = { { B::x }, { B } }
|
| 296 |
struct C: public A, public B { }; // S(x,C) = { invalid, { A in C, B in C } }
|
| 297 |
struct D: public virtual C { }; // S(x,D) = S(x,C)
|
|
@@ -301,18 +343,22 @@ int main() {
|
|
| 301 |
F f;
|
| 302 |
f.x = 0; // OK, lookup finds E::x
|
| 303 |
}
|
| 304 |
```
|
| 305 |
|
| 306 |
-
S(x,F) is unambiguous because the `A` and `B` base subobjects of
|
| 307 |
-
also base subobjects of `E`, so S(x,D) is discarded in the
|
| 308 |
-
step.
|
| 309 |
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
|
| 315 |
``` cpp
|
| 316 |
struct A {
|
| 317 |
int f();
|
| 318 |
};
|
|
@@ -328,14 +374,19 @@ struct B {
|
|
| 328 |
struct C : A, B {
|
| 329 |
int f() { return A::f() + B::f(); }
|
| 330 |
};
|
| 331 |
```
|
| 332 |
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
|
| 338 |
``` cpp
|
| 339 |
struct V {
|
| 340 |
int v;
|
| 341 |
};
|
|
@@ -354,15 +405,20 @@ void f(D* pd) {
|
|
| 354 |
int i = pd->e; // OK: only one e (enumerator)
|
| 355 |
pd->a++; // error, ambiguous: two a{s} in D
|
| 356 |
}
|
| 357 |
```
|
| 358 |
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
``` cpp
|
| 366 |
struct V { int f(); int x; };
|
| 367 |
struct W { int g(); int y; };
|
| 368 |
struct B : virtual V, W {
|
|
@@ -389,15 +445,19 @@ void D::glorp() {
|
|
| 389 |
y++; // error: B::y and C's W::y
|
| 390 |
g(); // error: B::g() and C's W::g()
|
| 391 |
}
|
| 392 |
```
|
| 393 |
|
|
|
|
|
|
|
| 394 |
An explicit or implicit conversion from a pointer to or an expression
|
| 395 |
designating an object of a derived class to a pointer or reference to
|
| 396 |
one of its base classes shall unambiguously refer to a unique object
|
| 397 |
representing the base class.
|
| 398 |
|
|
|
|
|
|
|
| 399 |
``` cpp
|
| 400 |
struct V { };
|
| 401 |
struct A { };
|
| 402 |
struct B : A, virtual V { };
|
| 403 |
struct C : A, virtual V { };
|
|
@@ -409,13 +469,17 @@ void g() {
|
|
| 409 |
A* pa = &d; // error, ambiguous: C's A or B's A?
|
| 410 |
V* pv = &d; // OK: only one V subobject
|
| 411 |
}
|
| 412 |
```
|
| 413 |
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
|
| 418 |
``` cpp
|
| 419 |
struct B1 {
|
| 420 |
void f();
|
| 421 |
static void f(int);
|
|
@@ -438,15 +502,19 @@ struct D: I1, I2, B2 {
|
|
| 438 |
int D::* mpD = &D::i; // Ambiguous conversion
|
| 439 |
}
|
| 440 |
};
|
| 441 |
```
|
| 442 |
|
|
|
|
|
|
|
| 443 |
## Virtual functions <a id="class.virtual">[[class.virtual]]</a>
|
| 444 |
|
| 445 |
-
Virtual functions support dynamic binding and
|
| 446 |
-
programming.
|
| 447 |
-
|
|
|
|
|
|
|
| 448 |
|
| 449 |
If a virtual member function `vf` is declared in a class `Base` and in a
|
| 450 |
class `Derived`, derived directly or indirectly from `Base`, a member
|
| 451 |
function `vf` with the same name, parameter-type-list ([[dcl.fct]]),
|
| 452 |
cv-qualification, and ref-qualifier (or absence of same) as `Base::vf`
|
|
@@ -457,10 +525,12 @@ of a class object `S` is a *final overrider* unless the most derived
|
|
| 457 |
class ([[intro.object]]) of which `S` is a base class subobject (if
|
| 458 |
any) declares or inherits another member function that overrides `vf`.
|
| 459 |
In a derived class, if a virtual member function of a base class
|
| 460 |
subobject has more than one final overrider the program is ill-formed.
|
| 461 |
|
|
|
|
|
|
|
| 462 |
``` cpp
|
| 463 |
struct A {
|
| 464 |
virtual void f();
|
| 465 |
};
|
| 466 |
struct B : virtual A {
|
|
@@ -475,18 +545,26 @@ void foo() {
|
|
| 475 |
c.f(); // calls B::f, the final overrider
|
| 476 |
c.C::f(); // calls A::f because of the using-declaration
|
| 477 |
}
|
| 478 |
```
|
| 479 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
``` cpp
|
| 481 |
struct A { virtual void f(); };
|
| 482 |
struct B : A { };
|
| 483 |
struct C : A { void f(); };
|
| 484 |
struct D : B, C { }; // OK: A::f and C::f are the final overriders
|
| 485 |
// for the B and C subobjects, respectively
|
| 486 |
```
|
| 487 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 488 |
A virtual member function does not have to be visible to be overridden,
|
| 489 |
for example,
|
| 490 |
|
| 491 |
``` cpp
|
| 492 |
struct B {
|
|
@@ -504,28 +582,36 @@ the function `f(int)` in class `D` hides the virtual function `f()` in
|
|
| 504 |
its base class `B`; `D::f(int)` is not a virtual function. However,
|
| 505 |
`f()` declared in class `D2` has the same name and the same parameter
|
| 506 |
list as `B::f()`, and therefore is a virtual function that overrides the
|
| 507 |
function `B::f()` even though `B::f()` is not visible in class `D2`.
|
| 508 |
|
|
|
|
|
|
|
| 509 |
If a virtual function `f` in some class `B` is marked with the
|
| 510 |
*virt-specifier* `final` and in a class `D` derived from `B` a function
|
| 511 |
`D::f` overrides `B::f`, the program is ill-formed.
|
| 512 |
|
|
|
|
|
|
|
| 513 |
``` cpp
|
| 514 |
struct B {
|
| 515 |
virtual void f() const final;
|
| 516 |
};
|
| 517 |
|
| 518 |
struct D : B {
|
| 519 |
void f() const; // error: D::f attempts to override final B::f
|
| 520 |
};
|
| 521 |
```
|
| 522 |
|
|
|
|
|
|
|
| 523 |
If a virtual function is marked with the *virt-specifier* `override` and
|
| 524 |
does not override a member function of a base class, the program is
|
| 525 |
ill-formed.
|
| 526 |
|
|
|
|
|
|
|
| 527 |
``` cpp
|
| 528 |
struct B {
|
| 529 |
virtual void f(int);
|
| 530 |
};
|
| 531 |
|
|
@@ -533,10 +619,12 @@ struct D : B {
|
|
| 533 |
virtual void f(long) override; // error: wrong signature overriding B::f
|
| 534 |
virtual void f(int) override; // OK
|
| 535 |
};
|
| 536 |
```
|
| 537 |
|
|
|
|
|
|
|
| 538 |
Even though destructors are not inherited, a destructor in a derived
|
| 539 |
class overrides a base class destructor declared virtual; see
|
| 540 |
[[class.dtor]] and [[class.free]].
|
| 541 |
|
| 542 |
The return type of an overriding function shall be either identical to
|
|
@@ -561,10 +649,12 @@ that of `B::f`, the class type in the return type of `D::f` shall be
|
|
| 561 |
complete at the point of declaration of `D::f` or shall be the class
|
| 562 |
type `D`. When the overriding function is called as the final overrider
|
| 563 |
of the overridden function, its result is converted to the type returned
|
| 564 |
by the (statically chosen) overridden function ([[expr.call]]).
|
| 565 |
|
|
|
|
|
|
|
| 566 |
``` cpp
|
| 567 |
class B { };
|
| 568 |
class D : private B { friend class Derived; };
|
| 569 |
struct Base {
|
| 570 |
virtual void vf1();
|
|
@@ -603,27 +693,32 @@ void g() {
|
|
| 603 |
// convert the result to B*
|
| 604 |
dp->vf2(); // ill-formed: argument mismatch
|
| 605 |
}
|
| 606 |
```
|
| 607 |
|
| 608 |
-
|
| 609 |
-
of the object for which it is called (the dynamic type), whereas the
|
| 610 |
-
interpretation of a call of a non-virtual member function depends only
|
| 611 |
-
on the type of the pointer or reference denoting that object (the static
|
| 612 |
-
type) ([[expr.call]]).
|
| 613 |
|
| 614 |
-
The
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 619 |
|
| 620 |
A virtual function declared in a class shall be defined, or declared
|
| 621 |
-
pure ([[class.abstract]]) in that class, or both;
|
| 622 |
required ([[basic.def.odr]]).
|
| 623 |
|
| 624 |
-
|
|
|
|
|
|
|
| 625 |
|
| 626 |
``` cpp
|
| 627 |
struct A {
|
| 628 |
virtual void f();
|
| 629 |
};
|
|
@@ -653,10 +748,14 @@ void foo() {
|
|
| 653 |
In class `D` above there are two occurrences of class `A` and hence two
|
| 654 |
occurrences of the virtual member function `A::f`. The final overrider
|
| 655 |
of `B1::A::f` is `B1::f` and the final overrider of `B2::A::f` is
|
| 656 |
`B2::f`.
|
| 657 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 658 |
The following example shows a function that does not have a unique final
|
| 659 |
overrider:
|
| 660 |
|
| 661 |
``` cpp
|
| 662 |
struct A {
|
|
@@ -682,10 +781,14 @@ struct Okay : VB1, VB2 {
|
|
| 682 |
Both `VB1::f` and `VB2::f` override `A::f` but there is no overrider of
|
| 683 |
both of them in class `Error`. This example is therefore ill-formed.
|
| 684 |
Class `Okay` is well formed, however, because `Okay::f` is a final
|
| 685 |
overrider.
|
| 686 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 687 |
The following example uses the well-formed classes from above.
|
| 688 |
|
| 689 |
``` cpp
|
| 690 |
struct VB1a : virtual A { // does not declare f
|
| 691 |
};
|
|
@@ -697,82 +800,105 @@ void foe() {
|
|
| 697 |
VB1a* vb1ap = new Da;
|
| 698 |
vb1ap->f(); // calls VB2::f
|
| 699 |
}
|
| 700 |
```
|
| 701 |
|
|
|
|
|
|
|
| 702 |
Explicit qualification with the scope operator ([[expr.prim]])
|
| 703 |
suppresses the virtual call mechanism.
|
| 704 |
|
|
|
|
|
|
|
| 705 |
``` cpp
|
| 706 |
class B { public: virtual void f(); };
|
| 707 |
class D : public B { public: void f(); };
|
| 708 |
|
| 709 |
-
void D::f() {
|
| 710 |
```
|
| 711 |
|
| 712 |
Here, the function call in `D::f` really does call `B::f` and not
|
| 713 |
`D::f`.
|
| 714 |
|
|
|
|
|
|
|
| 715 |
A function with a deleted definition ([[dcl.fct.def]]) shall not
|
| 716 |
override a function that does not have a deleted definition. Likewise, a
|
| 717 |
function that does not have a deleted definition shall not override a
|
| 718 |
function with a deleted definition.
|
| 719 |
|
| 720 |
## Abstract classes <a id="class.abstract">[[class.abstract]]</a>
|
| 721 |
|
| 722 |
-
The abstract class mechanism supports the notion of a
|
| 723 |
-
such as a `shape`, of which only more concrete
|
| 724 |
-
`circle` and `square`, can actually be used. An
|
| 725 |
-
be used to define an interface for which derived
|
| 726 |
-
variety of implementations.
|
| 727 |
|
| 728 |
An *abstract class* is a class that can be used only as a base class of
|
| 729 |
some other class; no objects of an abstract class can be created except
|
| 730 |
as subobjects of a class derived from it. A class is abstract if it has
|
| 731 |
-
at least one *pure virtual function*.
|
| 732 |
-
|
| 733 |
-
*
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 737 |
|
| 738 |
``` cpp
|
| 739 |
-
class point {
|
| 740 |
class shape { // abstract class
|
| 741 |
point center;
|
| 742 |
public:
|
| 743 |
point where() { return center; }
|
| 744 |
void move(point p) { center=p; draw(); }
|
| 745 |
virtual void rotate(int) = 0; // pure virtual
|
| 746 |
virtual void draw() = 0; // pure virtual
|
| 747 |
};
|
| 748 |
```
|
| 749 |
|
| 750 |
-
|
| 751 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 752 |
|
| 753 |
``` cpp
|
| 754 |
struct C {
|
| 755 |
virtual void f() = 0 { }; // ill-formed
|
| 756 |
};
|
| 757 |
```
|
| 758 |
|
|
|
|
|
|
|
| 759 |
An abstract class shall not be used as a parameter type, as a function
|
| 760 |
return type, or as the type of an explicit conversion. Pointers and
|
| 761 |
references to an abstract class can be declared.
|
| 762 |
|
|
|
|
|
|
|
| 763 |
``` cpp
|
| 764 |
shape x; // error: object of abstract class
|
| 765 |
shape* p; // OK
|
| 766 |
shape f(); // error
|
| 767 |
void g(shape); // error
|
| 768 |
shape& h(shape&); // OK
|
| 769 |
```
|
| 770 |
|
|
|
|
|
|
|
| 771 |
A class is abstract if it contains or inherits at least one pure virtual
|
| 772 |
function for which the final overrider is pure virtual.
|
| 773 |
|
|
|
|
|
|
|
| 774 |
``` cpp
|
| 775 |
class ab_circle : public shape {
|
| 776 |
int radius;
|
| 777 |
public:
|
| 778 |
void rotate(int) { }
|
|
@@ -790,16 +916,18 @@ public:
|
|
| 790 |
void rotate(int) { }
|
| 791 |
void draw(); // a definition is required somewhere
|
| 792 |
};
|
| 793 |
```
|
| 794 |
|
| 795 |
-
would make class `circle`
|
| 796 |
`circle::draw()` must be provided.
|
| 797 |
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
|
|
|
|
|
|
| 801 |
|
| 802 |
Member functions can be called from a constructor (or destructor) of an
|
| 803 |
abstract class; the effect of making a virtual call ([[class.virtual]])
|
| 804 |
to a pure virtual function directly or indirectly for the object being
|
| 805 |
created (or destroyed) from such a constructor (or destructor) is
|
|
|
|
| 14 |
base-specifier-list ',' base-specifier '...'ₒₚₜ
|
| 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ₒₚₜ class-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 class type that is not an
|
| 42 |
+
incompletely defined class (Clause [[class]]). The class denoted by the
|
| 43 |
+
*class-or-decltype* of a *base-specifier* is called a *direct base
|
| 44 |
+
class* for the class being defined. During the lookup for a base class
|
| 45 |
+
name, non-type names are ignored ([[basic.scope.hiding]]). If the name
|
| 46 |
+
found is not a *class-name*, the program is ill-formed. A class `B` is a
|
| 47 |
+
base class of a class `D` if it is a direct base class of `D` or a
|
| 48 |
+
direct base class of one of `D`’s base classes. A class is an *indirect*
|
| 49 |
+
base class of another if it is a base class but not a direct base class.
|
| 50 |
+
A class is said to be (directly or indirectly) *derived* from its
|
| 51 |
+
(direct or indirect) base classes.
|
| 52 |
+
|
| 53 |
+
[*Note 1*: See Clause [[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]]) can be
|
| 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 ([[conv.ptr]]). An lvalue of a derived class type can be bound to
|
| 71 |
+
a 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*:
|
| 78 |
+
|
| 79 |
``` cpp
|
| 80 |
struct Base {
|
| 81 |
int a, b, c;
|
| 82 |
};
|
| 83 |
```
|
|
|
|
| 95 |
```
|
| 96 |
|
| 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”. An arrow need not have a physical
|
| 111 |
+
representation in memory. A DAG of subobjects is often referred to as a
|
| 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 ([[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 (
|
| 126 |
+
[[class.cdtor]]) different from the polymorphic behavior of a most
|
| 127 |
+
derived object of the same type. A base class subobject may be of zero
|
| 128 |
+
size (Clause [[class]]); however, two subobjects that have the same
|
| 129 |
+
class type and that belong to the same most derived object must not be
|
| 130 |
+
allocated at the same address ([[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*]
|
| 138 |
+
|
| 139 |
+
[*Example 1*:
|
| 140 |
|
| 141 |
``` cpp
|
| 142 |
+
class A { ... };
|
| 143 |
+
class B { ... };
|
| 144 |
+
class C { ... };
|
| 145 |
+
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]]), 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 { ... };
|
| 169 |
+
class Y : public X, public X { ... }; // ill-formed
|
| 170 |
```
|
| 171 |
|
| 172 |
``` cpp
|
| 173 |
+
class L { public: int next; ... };
|
| 174 |
+
class A : public L { ... };
|
| 175 |
+
class B : public L { ... };
|
| 176 |
+
class C : public A, public B { void f(); ... }; // well-formed
|
| 177 |
+
class D : public A, public L { void f(); ... }; // well-formed
|
| 178 |
```
|
| 179 |
|
| 180 |
+
— *end example*]
|
| 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:nonvirt]].
|
| 199 |
|
| 200 |
<a id="fig:nonvirt"></a>
|
| 201 |
|
| 202 |
![Non-virtual base \[fig:nonvirt\]](images/fignonvirt.svg)
|
| 203 |
|
|
|
|
| 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 |
+
|
| 219 |
+
In contrast, consider the case with a virtual base class:
|
| 220 |
|
| 221 |
``` cpp
|
| 222 |
+
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 |
+
|
| 242 |
A class can have both virtual and non-virtual base classes of a given
|
| 243 |
type.
|
| 244 |
|
| 245 |
``` cpp
|
| 246 |
+
class B { ... };
|
| 247 |
+
class X : virtual public B { ... };
|
| 248 |
+
class Y : virtual public B { ... };
|
| 249 |
+
class Z : public B { ... };
|
| 250 |
+
class AA : public X, public Y, public Z { ... };
|
| 251 |
```
|
| 252 |
|
| 253 |
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 |
## Member name lookup <a id="class.member.lookup">[[class.member.lookup]]</a>
|
| 269 |
|
| 270 |
Member name lookup determines the meaning of a name (*id-expression*) in
|
| 271 |
a class scope ([[basic.scope.class]]). Name lookup can result in an
|
| 272 |
*ambiguity*, in which case the program is ill-formed. For an
|
|
|
|
| 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
|
|
|
|
| 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)
|
|
|
|
| 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 |
};
|
|
|
|
| 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 |
};
|
|
|
|
| 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 {
|
|
|
|
| 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 { };
|
|
|
|
| 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);
|
|
|
|
| 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 ([[dcl.fct]]),
|
| 520 |
cv-qualification, and ref-qualifier (or absence of same) as `Base::vf`
|
|
|
|
| 525 |
class ([[intro.object]]) of which `S` is a base class subobject (if
|
| 526 |
any) declares or inherits another member function that overrides `vf`.
|
| 527 |
In a derived class, if a virtual member function of a base class
|
| 528 |
subobject has more than one final overrider the program is ill-formed.
|
| 529 |
|
| 530 |
+
[*Example 1*:
|
| 531 |
+
|
| 532 |
``` cpp
|
| 533 |
struct A {
|
| 534 |
virtual void f();
|
| 535 |
};
|
| 536 |
struct B : virtual A {
|
|
|
|
| 545 |
c.f(); // calls B::f, the final overrider
|
| 546 |
c.C::f(); // calls A::f because of the using-declaration
|
| 547 |
}
|
| 548 |
```
|
| 549 |
|
| 550 |
+
— *end example*]
|
| 551 |
+
|
| 552 |
+
[*Example 2*:
|
| 553 |
+
|
| 554 |
``` cpp
|
| 555 |
struct A { virtual void f(); };
|
| 556 |
struct B : A { };
|
| 557 |
struct C : A { void f(); };
|
| 558 |
struct D : B, C { }; // OK: A::f and C::f are the final overriders
|
| 559 |
// for the B and C subobjects, respectively
|
| 560 |
```
|
| 561 |
|
| 562 |
+
— *end example*]
|
| 563 |
+
|
| 564 |
+
[*Note 2*:
|
| 565 |
+
|
| 566 |
A virtual member function does not have to be visible to be overridden,
|
| 567 |
for example,
|
| 568 |
|
| 569 |
``` cpp
|
| 570 |
struct B {
|
|
|
|
| 582 |
its base class `B`; `D::f(int)` is not a virtual function. However,
|
| 583 |
`f()` declared in class `D2` has the same name and the same parameter
|
| 584 |
list as `B::f()`, and therefore is a virtual function that overrides the
|
| 585 |
function `B::f()` even though `B::f()` is not visible in class `D2`.
|
| 586 |
|
| 587 |
+
— *end note*]
|
| 588 |
+
|
| 589 |
If a virtual function `f` in some class `B` is marked with the
|
| 590 |
*virt-specifier* `final` and in a class `D` derived from `B` a function
|
| 591 |
`D::f` overrides `B::f`, the program is ill-formed.
|
| 592 |
|
| 593 |
+
[*Example 3*:
|
| 594 |
+
|
| 595 |
``` cpp
|
| 596 |
struct B {
|
| 597 |
virtual void f() const final;
|
| 598 |
};
|
| 599 |
|
| 600 |
struct D : B {
|
| 601 |
void f() const; // error: D::f attempts to override final B::f
|
| 602 |
};
|
| 603 |
```
|
| 604 |
|
| 605 |
+
— *end example*]
|
| 606 |
+
|
| 607 |
If a virtual function is marked with the *virt-specifier* `override` and
|
| 608 |
does not override a member function of a base class, the program is
|
| 609 |
ill-formed.
|
| 610 |
|
| 611 |
+
[*Example 4*:
|
| 612 |
+
|
| 613 |
``` cpp
|
| 614 |
struct B {
|
| 615 |
virtual void f(int);
|
| 616 |
};
|
| 617 |
|
|
|
|
| 619 |
virtual void f(long) override; // error: wrong signature overriding B::f
|
| 620 |
virtual void f(int) override; // OK
|
| 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
|
|
|
|
| 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 ([[expr.call]]).
|
| 653 |
|
| 654 |
+
[*Example 5*:
|
| 655 |
+
|
| 656 |
``` cpp
|
| 657 |
class B { };
|
| 658 |
class D : private B { friend class Derived; };
|
| 659 |
struct Base {
|
| 660 |
virtual void vf1();
|
|
|
|
| 693 |
// convert the result to B*
|
| 694 |
dp->vf2(); // ill-formed: argument mismatch
|
| 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) ([[expr.call]]). — *end note*]
|
| 705 |
+
|
| 706 |
+
[*Note 4*: The `virtual` specifier implies membership, so a virtual
|
| 707 |
+
function cannot be a non-member ([[dcl.fct.spec]]) function. Nor can a
|
| 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 `friend` in
|
| 711 |
+
another class. — *end note*]
|
| 712 |
|
| 713 |
A virtual function declared in a class shall be defined, or declared
|
| 714 |
+
pure ([[class.abstract]]) in that class, or both; no diagnostic is
|
| 715 |
required ([[basic.def.odr]]).
|
| 716 |
|
| 717 |
+
[*Example 6*:
|
| 718 |
+
|
| 719 |
+
Here are some uses of virtual functions with multiple base classes:
|
| 720 |
|
| 721 |
``` cpp
|
| 722 |
struct A {
|
| 723 |
virtual void f();
|
| 724 |
};
|
|
|
|
| 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 7*:
|
| 756 |
+
|
| 757 |
The following example shows a function that does not have a unique final
|
| 758 |
overrider:
|
| 759 |
|
| 760 |
``` cpp
|
| 761 |
struct A {
|
|
|
|
| 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 formed, however, because `Okay::f` is a final
|
| 784 |
overrider.
|
| 785 |
|
| 786 |
+
— *end example*]
|
| 787 |
+
|
| 788 |
+
[*Example 8*:
|
| 789 |
+
|
| 790 |
The following example uses the well-formed classes from above.
|
| 791 |
|
| 792 |
``` cpp
|
| 793 |
struct VB1a : virtual A { // does not declare f
|
| 794 |
};
|
|
|
|
| 800 |
VB1a* vb1ap = new Da;
|
| 801 |
vb1ap->f(); // calls VB2::f
|
| 802 |
}
|
| 803 |
```
|
| 804 |
|
| 805 |
+
— *end example*]
|
| 806 |
+
|
| 807 |
Explicit qualification with the scope operator ([[expr.prim]])
|
| 808 |
suppresses the virtual call mechanism.
|
| 809 |
|
| 810 |
+
[*Example 9*:
|
| 811 |
+
|
| 812 |
``` cpp
|
| 813 |
class B { public: virtual void f(); };
|
| 814 |
class D : public B { public: void f(); };
|
| 815 |
|
| 816 |
+
void D::f() { ... B::f(); }
|
| 817 |
```
|
| 818 |
|
| 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 ([[dcl.fct.def]]) shall not
|
| 825 |
override a function that does not have a deleted definition. Likewise, a
|
| 826 |
function that does not have a deleted definition shall not override a
|
| 827 |
function with a deleted definition.
|
| 828 |
|
| 829 |
## Abstract classes <a id="class.abstract">[[class.abstract]]</a>
|
| 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 |
An *abstract class* is a class that can be used only as a base class of
|
| 838 |
some other class; no objects of an abstract class can be created except
|
| 839 |
as subobjects of a class derived from it. A class is abstract if it has
|
| 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 virtual function is specified *pure* by using a *pure-specifier* (
|
| 846 |
+
[[class.mem]]) in the function declaration in the class definition. A
|
| 847 |
+
pure virtual function need be defined only if called with, or as if
|
| 848 |
+
with ([[class.dtor]]), the *qualified-id* syntax ([[expr.prim]]).
|
| 849 |
+
|
| 850 |
+
[*Example 1*:
|
| 851 |
|
| 852 |
``` cpp
|
| 853 |
+
class point { ... };
|
| 854 |
class shape { // abstract class
|
| 855 |
point center;
|
| 856 |
public:
|
| 857 |
point where() { return center; }
|
| 858 |
void move(point p) { center=p; draw(); }
|
| 859 |
virtual void rotate(int) = 0; // pure virtual
|
| 860 |
virtual void draw() = 0; // pure virtual
|
| 861 |
};
|
| 862 |
```
|
| 863 |
|
| 864 |
+
— *end example*]
|
| 865 |
+
|
| 866 |
+
[*Note 3*: A function declaration cannot provide both a
|
| 867 |
+
*pure-specifier* and a definition — *end note*]
|
| 868 |
+
|
| 869 |
+
[*Example 2*:
|
| 870 |
|
| 871 |
``` cpp
|
| 872 |
struct C {
|
| 873 |
virtual void f() = 0 { }; // ill-formed
|
| 874 |
};
|
| 875 |
```
|
| 876 |
|
| 877 |
+
— *end example*]
|
| 878 |
+
|
| 879 |
An abstract class shall not be used as a parameter type, as a function
|
| 880 |
return type, or as the type of an explicit conversion. Pointers and
|
| 881 |
references to an abstract class can be declared.
|
| 882 |
|
| 883 |
+
[*Example 3*:
|
| 884 |
+
|
| 885 |
``` cpp
|
| 886 |
shape x; // error: object of abstract class
|
| 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 4*:
|
| 899 |
+
|
| 900 |
``` cpp
|
| 901 |
class ab_circle : public shape {
|
| 902 |
int radius;
|
| 903 |
public:
|
| 904 |
void rotate(int) { }
|
|
|
|
| 916 |
void rotate(int) { }
|
| 917 |
void draw(); // a definition is required somewhere
|
| 918 |
};
|
| 919 |
```
|
| 920 |
|
| 921 |
+
would make class `circle` non-abstract and a definition of
|
| 922 |
`circle::draw()` must be provided.
|
| 923 |
|
| 924 |
+
— *end example*]
|
| 925 |
+
|
| 926 |
+
[*Note 4*: An abstract class can be derived from a class that is not
|
| 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 ([[class.virtual]])
|
| 932 |
to a pure virtual function directly or indirectly for the object being
|
| 933 |
created (or destroyed) from such a constructor (or destructor) is
|