tmp/tmpfxhk88ws/{from.md → to.md}
RENAMED
|
@@ -1,59 +1,76 @@
|
|
| 1 |
## Abstract classes <a id="class.abstract">[[class.abstract]]</a>
|
| 2 |
|
| 3 |
-
The abstract class mechanism supports the notion of a
|
| 4 |
-
such as a `shape`, of which only more concrete
|
| 5 |
-
`circle` and `square`, can actually be used. An
|
| 6 |
-
be used to define an interface for which derived
|
| 7 |
-
variety of implementations.
|
| 8 |
|
| 9 |
An *abstract class* is a class that can be used only as a base class of
|
| 10 |
some other class; no objects of an abstract class can be created except
|
| 11 |
as subobjects of a class derived from it. A class is abstract if it has
|
| 12 |
-
at least one *pure virtual function*.
|
| 13 |
-
|
| 14 |
-
*
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
``` cpp
|
| 20 |
-
class point {
|
| 21 |
class shape { // abstract class
|
| 22 |
point center;
|
| 23 |
public:
|
| 24 |
point where() { return center; }
|
| 25 |
void move(point p) { center=p; draw(); }
|
| 26 |
virtual void rotate(int) = 0; // pure virtual
|
| 27 |
virtual void draw() = 0; // pure virtual
|
| 28 |
};
|
| 29 |
```
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
``` cpp
|
| 35 |
struct C {
|
| 36 |
virtual void f() = 0 { }; // ill-formed
|
| 37 |
};
|
| 38 |
```
|
| 39 |
|
|
|
|
|
|
|
| 40 |
An abstract class shall not be used as a parameter type, as a function
|
| 41 |
return type, or as the type of an explicit conversion. Pointers and
|
| 42 |
references to an abstract class can be declared.
|
| 43 |
|
|
|
|
|
|
|
| 44 |
``` cpp
|
| 45 |
shape x; // error: object of abstract class
|
| 46 |
shape* p; // OK
|
| 47 |
shape f(); // error
|
| 48 |
void g(shape); // error
|
| 49 |
shape& h(shape&); // OK
|
| 50 |
```
|
| 51 |
|
|
|
|
|
|
|
| 52 |
A class is abstract if it contains or inherits at least one pure virtual
|
| 53 |
function for which the final overrider is pure virtual.
|
| 54 |
|
|
|
|
|
|
|
| 55 |
``` cpp
|
| 56 |
class ab_circle : public shape {
|
| 57 |
int radius;
|
| 58 |
public:
|
| 59 |
void rotate(int) { }
|
|
@@ -71,16 +88,18 @@ public:
|
|
| 71 |
void rotate(int) { }
|
| 72 |
void draw(); // a definition is required somewhere
|
| 73 |
};
|
| 74 |
```
|
| 75 |
|
| 76 |
-
would make class `circle`
|
| 77 |
`circle::draw()` must be provided.
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
|
| 83 |
Member functions can be called from a constructor (or destructor) of an
|
| 84 |
abstract class; the effect of making a virtual call ([[class.virtual]])
|
| 85 |
to a pure virtual function directly or indirectly for the object being
|
| 86 |
created (or destroyed) from such a constructor (or destructor) is
|
|
|
|
| 1 |
## Abstract classes <a id="class.abstract">[[class.abstract]]</a>
|
| 2 |
|
| 3 |
+
[*Note 1*: The abstract class mechanism supports the notion of a
|
| 4 |
+
general concept, such as a `shape`, of which only more concrete
|
| 5 |
+
variants, such as `circle` and `square`, can actually be used. An
|
| 6 |
+
abstract class can also be used to define an interface for which derived
|
| 7 |
+
classes provide a variety of implementations. — *end note*]
|
| 8 |
|
| 9 |
An *abstract class* is a class that can be used only as a base class of
|
| 10 |
some other class; no objects of an abstract class can be created except
|
| 11 |
as subobjects of a class derived from it. A class is abstract if it has
|
| 12 |
+
at least one *pure virtual function*.
|
| 13 |
+
|
| 14 |
+
[*Note 2*: Such a function might be inherited: see
|
| 15 |
+
below. — *end note*]
|
| 16 |
+
|
| 17 |
+
A virtual function is specified *pure* by using a *pure-specifier* (
|
| 18 |
+
[[class.mem]]) in the function declaration in the class definition. A
|
| 19 |
+
pure virtual function need be defined only if called with, or as if
|
| 20 |
+
with ([[class.dtor]]), the *qualified-id* syntax ([[expr.prim]]).
|
| 21 |
+
|
| 22 |
+
[*Example 1*:
|
| 23 |
|
| 24 |
``` cpp
|
| 25 |
+
class point { ... };
|
| 26 |
class shape { // abstract class
|
| 27 |
point center;
|
| 28 |
public:
|
| 29 |
point where() { return center; }
|
| 30 |
void move(point p) { center=p; draw(); }
|
| 31 |
virtual void rotate(int) = 0; // pure virtual
|
| 32 |
virtual void draw() = 0; // pure virtual
|
| 33 |
};
|
| 34 |
```
|
| 35 |
|
| 36 |
+
— *end example*]
|
| 37 |
+
|
| 38 |
+
[*Note 3*: A function declaration cannot provide both a
|
| 39 |
+
*pure-specifier* and a definition — *end note*]
|
| 40 |
+
|
| 41 |
+
[*Example 2*:
|
| 42 |
|
| 43 |
``` cpp
|
| 44 |
struct C {
|
| 45 |
virtual void f() = 0 { }; // ill-formed
|
| 46 |
};
|
| 47 |
```
|
| 48 |
|
| 49 |
+
— *end example*]
|
| 50 |
+
|
| 51 |
An abstract class shall not be used as a parameter type, as a function
|
| 52 |
return type, or as the type of an explicit conversion. Pointers and
|
| 53 |
references to an abstract class can be declared.
|
| 54 |
|
| 55 |
+
[*Example 3*:
|
| 56 |
+
|
| 57 |
``` cpp
|
| 58 |
shape x; // error: object of abstract class
|
| 59 |
shape* p; // OK
|
| 60 |
shape f(); // error
|
| 61 |
void g(shape); // error
|
| 62 |
shape& h(shape&); // OK
|
| 63 |
```
|
| 64 |
|
| 65 |
+
— *end example*]
|
| 66 |
+
|
| 67 |
A class is abstract if it contains or inherits at least one pure virtual
|
| 68 |
function for which the final overrider is pure virtual.
|
| 69 |
|
| 70 |
+
[*Example 4*:
|
| 71 |
+
|
| 72 |
``` cpp
|
| 73 |
class ab_circle : public shape {
|
| 74 |
int radius;
|
| 75 |
public:
|
| 76 |
void rotate(int) { }
|
|
|
|
| 88 |
void rotate(int) { }
|
| 89 |
void draw(); // a definition is required somewhere
|
| 90 |
};
|
| 91 |
```
|
| 92 |
|
| 93 |
+
would make class `circle` non-abstract and a definition of
|
| 94 |
`circle::draw()` must be provided.
|
| 95 |
|
| 96 |
+
— *end example*]
|
| 97 |
+
|
| 98 |
+
[*Note 4*: An abstract class can be derived from a class that is not
|
| 99 |
+
abstract, and a pure virtual function may override a virtual function
|
| 100 |
+
which is not pure. — *end note*]
|
| 101 |
|
| 102 |
Member functions can be called from a constructor (or destructor) of an
|
| 103 |
abstract class; the effect of making a virtual call ([[class.virtual]])
|
| 104 |
to a pure virtual function directly or indirectly for the object being
|
| 105 |
created (or destroyed) from such a constructor (or destructor) is
|