tmp/tmp2umh52rf/{from.md → to.md}
RENAMED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
## Virtual functions <a id="class.virtual">[[class.virtual]]</a>
|
| 2 |
|
| 3 |
-
Virtual functions support dynamic binding and
|
| 4 |
-
programming.
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
If a virtual member function `vf` is declared in a class `Base` and in a
|
| 8 |
class `Derived`, derived directly or indirectly from `Base`, a member
|
| 9 |
function `vf` with the same name, parameter-type-list ([[dcl.fct]]),
|
| 10 |
cv-qualification, and ref-qualifier (or absence of same) as `Base::vf`
|
|
@@ -15,10 +17,12 @@ of a class object `S` is a *final overrider* unless the most derived
|
|
| 15 |
class ([[intro.object]]) of which `S` is a base class subobject (if
|
| 16 |
any) declares or inherits another member function that overrides `vf`.
|
| 17 |
In a derived class, if a virtual member function of a base class
|
| 18 |
subobject has more than one final overrider the program is ill-formed.
|
| 19 |
|
|
|
|
|
|
|
| 20 |
``` cpp
|
| 21 |
struct A {
|
| 22 |
virtual void f();
|
| 23 |
};
|
| 24 |
struct B : virtual A {
|
|
@@ -33,18 +37,26 @@ void foo() {
|
|
| 33 |
c.f(); // calls B::f, the final overrider
|
| 34 |
c.C::f(); // calls A::f because of the using-declaration
|
| 35 |
}
|
| 36 |
```
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
``` cpp
|
| 39 |
struct A { virtual void f(); };
|
| 40 |
struct B : A { };
|
| 41 |
struct C : A { void f(); };
|
| 42 |
struct D : B, C { }; // OK: A::f and C::f are the final overriders
|
| 43 |
// for the B and C subobjects, respectively
|
| 44 |
```
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
A virtual member function does not have to be visible to be overridden,
|
| 47 |
for example,
|
| 48 |
|
| 49 |
``` cpp
|
| 50 |
struct B {
|
|
@@ -62,28 +74,36 @@ the function `f(int)` in class `D` hides the virtual function `f()` in
|
|
| 62 |
its base class `B`; `D::f(int)` is not a virtual function. However,
|
| 63 |
`f()` declared in class `D2` has the same name and the same parameter
|
| 64 |
list as `B::f()`, and therefore is a virtual function that overrides the
|
| 65 |
function `B::f()` even though `B::f()` is not visible in class `D2`.
|
| 66 |
|
|
|
|
|
|
|
| 67 |
If a virtual function `f` in some class `B` is marked with the
|
| 68 |
*virt-specifier* `final` and in a class `D` derived from `B` a function
|
| 69 |
`D::f` overrides `B::f`, the program is ill-formed.
|
| 70 |
|
|
|
|
|
|
|
| 71 |
``` cpp
|
| 72 |
struct B {
|
| 73 |
virtual void f() const final;
|
| 74 |
};
|
| 75 |
|
| 76 |
struct D : B {
|
| 77 |
void f() const; // error: D::f attempts to override final B::f
|
| 78 |
};
|
| 79 |
```
|
| 80 |
|
|
|
|
|
|
|
| 81 |
If a virtual function is marked with the *virt-specifier* `override` and
|
| 82 |
does not override a member function of a base class, the program is
|
| 83 |
ill-formed.
|
| 84 |
|
|
|
|
|
|
|
| 85 |
``` cpp
|
| 86 |
struct B {
|
| 87 |
virtual void f(int);
|
| 88 |
};
|
| 89 |
|
|
@@ -91,10 +111,12 @@ struct D : B {
|
|
| 91 |
virtual void f(long) override; // error: wrong signature overriding B::f
|
| 92 |
virtual void f(int) override; // OK
|
| 93 |
};
|
| 94 |
```
|
| 95 |
|
|
|
|
|
|
|
| 96 |
Even though destructors are not inherited, a destructor in a derived
|
| 97 |
class overrides a base class destructor declared virtual; see
|
| 98 |
[[class.dtor]] and [[class.free]].
|
| 99 |
|
| 100 |
The return type of an overriding function shall be either identical to
|
|
@@ -119,10 +141,12 @@ that of `B::f`, the class type in the return type of `D::f` shall be
|
|
| 119 |
complete at the point of declaration of `D::f` or shall be the class
|
| 120 |
type `D`. When the overriding function is called as the final overrider
|
| 121 |
of the overridden function, its result is converted to the type returned
|
| 122 |
by the (statically chosen) overridden function ([[expr.call]]).
|
| 123 |
|
|
|
|
|
|
|
| 124 |
``` cpp
|
| 125 |
class B { };
|
| 126 |
class D : private B { friend class Derived; };
|
| 127 |
struct Base {
|
| 128 |
virtual void vf1();
|
|
@@ -161,27 +185,32 @@ void g() {
|
|
| 161 |
// convert the result to B*
|
| 162 |
dp->vf2(); // ill-formed: argument mismatch
|
| 163 |
}
|
| 164 |
```
|
| 165 |
|
| 166 |
-
|
| 167 |
-
of the object for which it is called (the dynamic type), whereas the
|
| 168 |
-
interpretation of a call of a non-virtual member function depends only
|
| 169 |
-
on the type of the pointer or reference denoting that object (the static
|
| 170 |
-
type) ([[expr.call]]).
|
| 171 |
|
| 172 |
-
The
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
A virtual function declared in a class shall be defined, or declared
|
| 179 |
-
pure ([[class.abstract]]) in that class, or both;
|
| 180 |
required ([[basic.def.odr]]).
|
| 181 |
|
| 182 |
-
|
|
|
|
|
|
|
| 183 |
|
| 184 |
``` cpp
|
| 185 |
struct A {
|
| 186 |
virtual void f();
|
| 187 |
};
|
|
@@ -211,10 +240,14 @@ void foo() {
|
|
| 211 |
In class `D` above there are two occurrences of class `A` and hence two
|
| 212 |
occurrences of the virtual member function `A::f`. The final overrider
|
| 213 |
of `B1::A::f` is `B1::f` and the final overrider of `B2::A::f` is
|
| 214 |
`B2::f`.
|
| 215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
The following example shows a function that does not have a unique final
|
| 217 |
overrider:
|
| 218 |
|
| 219 |
``` cpp
|
| 220 |
struct A {
|
|
@@ -240,10 +273,14 @@ struct Okay : VB1, VB2 {
|
|
| 240 |
Both `VB1::f` and `VB2::f` override `A::f` but there is no overrider of
|
| 241 |
both of them in class `Error`. This example is therefore ill-formed.
|
| 242 |
Class `Okay` is well formed, however, because `Okay::f` is a final
|
| 243 |
overrider.
|
| 244 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
The following example uses the well-formed classes from above.
|
| 246 |
|
| 247 |
``` cpp
|
| 248 |
struct VB1a : virtual A { // does not declare f
|
| 249 |
};
|
|
@@ -255,23 +292,29 @@ void foe() {
|
|
| 255 |
VB1a* vb1ap = new Da;
|
| 256 |
vb1ap->f(); // calls VB2::f
|
| 257 |
}
|
| 258 |
```
|
| 259 |
|
|
|
|
|
|
|
| 260 |
Explicit qualification with the scope operator ([[expr.prim]])
|
| 261 |
suppresses the virtual call mechanism.
|
| 262 |
|
|
|
|
|
|
|
| 263 |
``` cpp
|
| 264 |
class B { public: virtual void f(); };
|
| 265 |
class D : public B { public: void f(); };
|
| 266 |
|
| 267 |
-
void D::f() {
|
| 268 |
```
|
| 269 |
|
| 270 |
Here, the function call in `D::f` really does call `B::f` and not
|
| 271 |
`D::f`.
|
| 272 |
|
|
|
|
|
|
|
| 273 |
A function with a deleted definition ([[dcl.fct.def]]) shall not
|
| 274 |
override a function that does not have a deleted definition. Likewise, a
|
| 275 |
function that does not have a deleted definition shall not override a
|
| 276 |
function with a deleted definition.
|
| 277 |
|
|
|
|
| 1 |
## Virtual functions <a id="class.virtual">[[class.virtual]]</a>
|
| 2 |
|
| 3 |
+
[*Note 1*: Virtual functions support dynamic binding and
|
| 4 |
+
object-oriented programming. — *end note*]
|
| 5 |
+
|
| 6 |
+
A class that declares or inherits a virtual function is called a
|
| 7 |
+
*polymorphic class*.
|
| 8 |
|
| 9 |
If a virtual member function `vf` is declared in a class `Base` and in a
|
| 10 |
class `Derived`, derived directly or indirectly from `Base`, a member
|
| 11 |
function `vf` with the same name, parameter-type-list ([[dcl.fct]]),
|
| 12 |
cv-qualification, and ref-qualifier (or absence of same) as `Base::vf`
|
|
|
|
| 17 |
class ([[intro.object]]) of which `S` is a base class subobject (if
|
| 18 |
any) declares or inherits another member function that overrides `vf`.
|
| 19 |
In a derived class, if a virtual member function of a base class
|
| 20 |
subobject has more than one final overrider the program is ill-formed.
|
| 21 |
|
| 22 |
+
[*Example 1*:
|
| 23 |
+
|
| 24 |
``` cpp
|
| 25 |
struct A {
|
| 26 |
virtual void f();
|
| 27 |
};
|
| 28 |
struct B : virtual A {
|
|
|
|
| 37 |
c.f(); // calls B::f, the final overrider
|
| 38 |
c.C::f(); // calls A::f because of the using-declaration
|
| 39 |
}
|
| 40 |
```
|
| 41 |
|
| 42 |
+
— *end example*]
|
| 43 |
+
|
| 44 |
+
[*Example 2*:
|
| 45 |
+
|
| 46 |
``` cpp
|
| 47 |
struct A { virtual void f(); };
|
| 48 |
struct B : A { };
|
| 49 |
struct C : A { void f(); };
|
| 50 |
struct D : B, C { }; // OK: A::f and C::f are the final overriders
|
| 51 |
// for the B and C subobjects, respectively
|
| 52 |
```
|
| 53 |
|
| 54 |
+
— *end example*]
|
| 55 |
+
|
| 56 |
+
[*Note 2*:
|
| 57 |
+
|
| 58 |
A virtual member function does not have to be visible to be overridden,
|
| 59 |
for example,
|
| 60 |
|
| 61 |
``` cpp
|
| 62 |
struct B {
|
|
|
|
| 74 |
its base class `B`; `D::f(int)` is not a virtual function. However,
|
| 75 |
`f()` declared in class `D2` has the same name and the same parameter
|
| 76 |
list as `B::f()`, and therefore is a virtual function that overrides the
|
| 77 |
function `B::f()` even though `B::f()` is not visible in class `D2`.
|
| 78 |
|
| 79 |
+
— *end note*]
|
| 80 |
+
|
| 81 |
If a virtual function `f` in some class `B` is marked with the
|
| 82 |
*virt-specifier* `final` and in a class `D` derived from `B` a function
|
| 83 |
`D::f` overrides `B::f`, the program is ill-formed.
|
| 84 |
|
| 85 |
+
[*Example 3*:
|
| 86 |
+
|
| 87 |
``` cpp
|
| 88 |
struct B {
|
| 89 |
virtual void f() const final;
|
| 90 |
};
|
| 91 |
|
| 92 |
struct D : B {
|
| 93 |
void f() const; // error: D::f attempts to override final B::f
|
| 94 |
};
|
| 95 |
```
|
| 96 |
|
| 97 |
+
— *end example*]
|
| 98 |
+
|
| 99 |
If a virtual function is marked with the *virt-specifier* `override` and
|
| 100 |
does not override a member function of a base class, the program is
|
| 101 |
ill-formed.
|
| 102 |
|
| 103 |
+
[*Example 4*:
|
| 104 |
+
|
| 105 |
``` cpp
|
| 106 |
struct B {
|
| 107 |
virtual void f(int);
|
| 108 |
};
|
| 109 |
|
|
|
|
| 111 |
virtual void f(long) override; // error: wrong signature overriding B::f
|
| 112 |
virtual void f(int) override; // OK
|
| 113 |
};
|
| 114 |
```
|
| 115 |
|
| 116 |
+
— *end example*]
|
| 117 |
+
|
| 118 |
Even though destructors are not inherited, a destructor in a derived
|
| 119 |
class overrides a base class destructor declared virtual; see
|
| 120 |
[[class.dtor]] and [[class.free]].
|
| 121 |
|
| 122 |
The return type of an overriding function shall be either identical to
|
|
|
|
| 141 |
complete at the point of declaration of `D::f` or shall be the class
|
| 142 |
type `D`. When the overriding function is called as the final overrider
|
| 143 |
of the overridden function, its result is converted to the type returned
|
| 144 |
by the (statically chosen) overridden function ([[expr.call]]).
|
| 145 |
|
| 146 |
+
[*Example 5*:
|
| 147 |
+
|
| 148 |
``` cpp
|
| 149 |
class B { };
|
| 150 |
class D : private B { friend class Derived; };
|
| 151 |
struct Base {
|
| 152 |
virtual void vf1();
|
|
|
|
| 185 |
// convert the result to B*
|
| 186 |
dp->vf2(); // ill-formed: argument mismatch
|
| 187 |
}
|
| 188 |
```
|
| 189 |
|
| 190 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
+
[*Note 3*: The interpretation of the call of a virtual function depends
|
| 193 |
+
on the type of the object for which it is called (the dynamic type),
|
| 194 |
+
whereas the interpretation of a call of a non-virtual member function
|
| 195 |
+
depends only on the type of the pointer or reference denoting that
|
| 196 |
+
object (the static type) ([[expr.call]]). — *end note*]
|
| 197 |
+
|
| 198 |
+
[*Note 4*: The `virtual` specifier implies membership, so a virtual
|
| 199 |
+
function cannot be a non-member ([[dcl.fct.spec]]) function. Nor can a
|
| 200 |
+
virtual function be a static member, since a virtual function call
|
| 201 |
+
relies on a specific object for determining which function to invoke. A
|
| 202 |
+
virtual function declared in one class can be declared a `friend` in
|
| 203 |
+
another class. — *end note*]
|
| 204 |
|
| 205 |
A virtual function declared in a class shall be defined, or declared
|
| 206 |
+
pure ([[class.abstract]]) in that class, or both; no diagnostic is
|
| 207 |
required ([[basic.def.odr]]).
|
| 208 |
|
| 209 |
+
[*Example 6*:
|
| 210 |
+
|
| 211 |
+
Here are some uses of virtual functions with multiple base classes:
|
| 212 |
|
| 213 |
``` cpp
|
| 214 |
struct A {
|
| 215 |
virtual void f();
|
| 216 |
};
|
|
|
|
| 240 |
In class `D` above there are two occurrences of class `A` and hence two
|
| 241 |
occurrences of the virtual member function `A::f`. The final overrider
|
| 242 |
of `B1::A::f` is `B1::f` and the final overrider of `B2::A::f` is
|
| 243 |
`B2::f`.
|
| 244 |
|
| 245 |
+
— *end example*]
|
| 246 |
+
|
| 247 |
+
[*Example 7*:
|
| 248 |
+
|
| 249 |
The following example shows a function that does not have a unique final
|
| 250 |
overrider:
|
| 251 |
|
| 252 |
``` cpp
|
| 253 |
struct A {
|
|
|
|
| 273 |
Both `VB1::f` and `VB2::f` override `A::f` but there is no overrider of
|
| 274 |
both of them in class `Error`. This example is therefore ill-formed.
|
| 275 |
Class `Okay` is well formed, however, because `Okay::f` is a final
|
| 276 |
overrider.
|
| 277 |
|
| 278 |
+
— *end example*]
|
| 279 |
+
|
| 280 |
+
[*Example 8*:
|
| 281 |
+
|
| 282 |
The following example uses the well-formed classes from above.
|
| 283 |
|
| 284 |
``` cpp
|
| 285 |
struct VB1a : virtual A { // does not declare f
|
| 286 |
};
|
|
|
|
| 292 |
VB1a* vb1ap = new Da;
|
| 293 |
vb1ap->f(); // calls VB2::f
|
| 294 |
}
|
| 295 |
```
|
| 296 |
|
| 297 |
+
— *end example*]
|
| 298 |
+
|
| 299 |
Explicit qualification with the scope operator ([[expr.prim]])
|
| 300 |
suppresses the virtual call mechanism.
|
| 301 |
|
| 302 |
+
[*Example 9*:
|
| 303 |
+
|
| 304 |
``` cpp
|
| 305 |
class B { public: virtual void f(); };
|
| 306 |
class D : public B { public: void f(); };
|
| 307 |
|
| 308 |
+
void D::f() { ... B::f(); }
|
| 309 |
```
|
| 310 |
|
| 311 |
Here, the function call in `D::f` really does call `B::f` and not
|
| 312 |
`D::f`.
|
| 313 |
|
| 314 |
+
— *end example*]
|
| 315 |
+
|
| 316 |
A function with a deleted definition ([[dcl.fct.def]]) shall not
|
| 317 |
override a function that does not have a deleted definition. Likewise, a
|
| 318 |
function that does not have a deleted definition shall not override a
|
| 319 |
function with a deleted definition.
|
| 320 |
|