tmp/tmpos3oy9b6/{from.md → to.md}
RENAMED
|
@@ -1,9 +1,11 @@
|
|
| 1 |
## Class names <a id="class.name">[[class.name]]</a>
|
| 2 |
|
| 3 |
A class definition introduces a new type.
|
| 4 |
|
|
|
|
|
|
|
| 5 |
``` cpp
|
| 6 |
struct X { int a; };
|
| 7 |
struct Y { int a; };
|
| 8 |
X a1;
|
| 9 |
Y a2;
|
|
@@ -32,54 +34,65 @@ struct S { int a; };
|
|
| 32 |
struct S { int a; }; // error, double definition
|
| 33 |
```
|
| 34 |
|
| 35 |
is ill-formed because it defines `S` twice.
|
| 36 |
|
|
|
|
|
|
|
| 37 |
A class declaration introduces the class name into the scope where it is
|
| 38 |
declared and hides any class, variable, function, or other declaration
|
| 39 |
of that name in an enclosing scope ([[basic.scope]]). If a class name
|
| 40 |
is declared in a scope where a variable, function, or enumerator of the
|
| 41 |
same name is also declared, then when both declarations are in scope,
|
| 42 |
the class can be referred to only using an *elaborated-type-specifier* (
|
| 43 |
[[basic.lookup.elab]]).
|
| 44 |
|
|
|
|
|
|
|
| 45 |
``` cpp
|
| 46 |
struct stat {
|
| 47 |
// ...
|
| 48 |
};
|
| 49 |
|
| 50 |
-
stat gstat; // use plain stat to
|
| 51 |
-
// define variable
|
| 52 |
|
| 53 |
int stat(struct stat*); // redeclare stat as function
|
| 54 |
|
| 55 |
void f() {
|
| 56 |
-
struct stat* ps; // struct prefix needed
|
| 57 |
-
// to name struct stat
|
| 58 |
stat(ps); // call stat()
|
| 59 |
}
|
| 60 |
```
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
``` cpp
|
| 68 |
struct s { int a; };
|
| 69 |
|
| 70 |
void g() {
|
| 71 |
-
struct s; // hide global struct s
|
| 72 |
-
// with a block-scope declaration
|
| 73 |
s* p; // refer to local struct s
|
| 74 |
struct s { char* p; }; // define local struct s
|
| 75 |
struct s; // redeclaration, has no effect
|
| 76 |
}
|
| 77 |
```
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
Such declarations allow definition of classes that refer to each other.
|
| 80 |
|
|
|
|
|
|
|
| 81 |
``` cpp
|
| 82 |
class Vector;
|
| 83 |
|
| 84 |
class Matrix {
|
| 85 |
// ...
|
|
@@ -93,24 +106,34 @@ class Vector {
|
|
| 93 |
```
|
| 94 |
|
| 95 |
Declaration of `friend`s is described in [[class.friend]], operator
|
| 96 |
functions in [[over.oper]].
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
``` cpp
|
| 104 |
struct s { int a; };
|
| 105 |
|
| 106 |
void g(int s) {
|
| 107 |
struct s* p = new struct s; // global s
|
| 108 |
p->a = s; // parameter s
|
| 109 |
}
|
| 110 |
```
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
The declaration of a class name takes effect immediately after the
|
| 113 |
*identifier* is seen in the class definition or
|
| 114 |
*elaborated-type-specifier*. For example,
|
| 115 |
|
| 116 |
``` cpp
|
|
@@ -120,10 +143,12 @@ class A * A;
|
|
| 120 |
first specifies `A` to be the name of a class and then redefines it as
|
| 121 |
the name of a pointer to an object of that class. This means that the
|
| 122 |
elaborated form `class` `A` must be used to refer to the class. Such
|
| 123 |
artistry with names can be confusing and is best avoided.
|
| 124 |
|
|
|
|
|
|
|
| 125 |
A *typedef-name* ([[dcl.typedef]]) that names a class type, or a
|
| 126 |
cv-qualified version thereof, is also a *class-name*. If a
|
| 127 |
*typedef-name* that names a cv-qualified class type is used where a
|
| 128 |
*class-name* is required, the cv-qualifiers are ignored. A
|
| 129 |
*typedef-name* shall not be used as the *identifier* in a *class-head*.
|
|
|
|
| 1 |
## Class names <a id="class.name">[[class.name]]</a>
|
| 2 |
|
| 3 |
A class definition introduces a new type.
|
| 4 |
|
| 5 |
+
[*Example 1*:
|
| 6 |
+
|
| 7 |
``` cpp
|
| 8 |
struct X { int a; };
|
| 9 |
struct Y { int a; };
|
| 10 |
X a1;
|
| 11 |
Y a2;
|
|
|
|
| 34 |
struct S { int a; }; // error, double definition
|
| 35 |
```
|
| 36 |
|
| 37 |
is ill-formed because it defines `S` twice.
|
| 38 |
|
| 39 |
+
— *end example*]
|
| 40 |
+
|
| 41 |
A class declaration introduces the class name into the scope where it is
|
| 42 |
declared and hides any class, variable, function, or other declaration
|
| 43 |
of that name in an enclosing scope ([[basic.scope]]). If a class name
|
| 44 |
is declared in a scope where a variable, function, or enumerator of the
|
| 45 |
same name is also declared, then when both declarations are in scope,
|
| 46 |
the class can be referred to only using an *elaborated-type-specifier* (
|
| 47 |
[[basic.lookup.elab]]).
|
| 48 |
|
| 49 |
+
[*Example 2*:
|
| 50 |
+
|
| 51 |
``` cpp
|
| 52 |
struct stat {
|
| 53 |
// ...
|
| 54 |
};
|
| 55 |
|
| 56 |
+
stat gstat; // use plain stat to define variable
|
|
|
|
| 57 |
|
| 58 |
int stat(struct stat*); // redeclare stat as function
|
| 59 |
|
| 60 |
void f() {
|
| 61 |
+
struct stat* ps; // struct prefix needed to name struct stat
|
|
|
|
| 62 |
stat(ps); // call stat()
|
| 63 |
}
|
| 64 |
```
|
| 65 |
|
| 66 |
+
— *end example*]
|
| 67 |
+
|
| 68 |
+
A *declaration* consisting solely of *class-key* *identifier*`;` is
|
| 69 |
+
either a redeclaration of the name in the current scope or a forward
|
| 70 |
+
declaration of the identifier as a class name. It introduces the class
|
| 71 |
+
name into the current scope.
|
| 72 |
+
|
| 73 |
+
[*Example 3*:
|
| 74 |
|
| 75 |
``` cpp
|
| 76 |
struct s { int a; };
|
| 77 |
|
| 78 |
void g() {
|
| 79 |
+
struct s; // hide global struct s with a block-scope declaration
|
|
|
|
| 80 |
s* p; // refer to local struct s
|
| 81 |
struct s { char* p; }; // define local struct s
|
| 82 |
struct s; // redeclaration, has no effect
|
| 83 |
}
|
| 84 |
```
|
| 85 |
|
| 86 |
+
— *end example*]
|
| 87 |
+
|
| 88 |
+
[*Note 1*:
|
| 89 |
+
|
| 90 |
Such declarations allow definition of classes that refer to each other.
|
| 91 |
|
| 92 |
+
[*Example 4*:
|
| 93 |
+
|
| 94 |
``` cpp
|
| 95 |
class Vector;
|
| 96 |
|
| 97 |
class Matrix {
|
| 98 |
// ...
|
|
|
|
| 106 |
```
|
| 107 |
|
| 108 |
Declaration of `friend`s is described in [[class.friend]], operator
|
| 109 |
functions in [[over.oper]].
|
| 110 |
|
| 111 |
+
— *end example*]
|
| 112 |
+
|
| 113 |
+
— *end note*]
|
| 114 |
+
|
| 115 |
+
[*Note 2*: An *elaborated-type-specifier* ([[dcl.type.elab]]) can also
|
| 116 |
+
be used as a *type-specifier* as part of a declaration. It differs from
|
| 117 |
+
a class declaration in that if a class of the elaborated name is in
|
| 118 |
+
scope the elaborated name will refer to it. — *end note*]
|
| 119 |
+
|
| 120 |
+
[*Example 5*:
|
| 121 |
|
| 122 |
``` cpp
|
| 123 |
struct s { int a; };
|
| 124 |
|
| 125 |
void g(int s) {
|
| 126 |
struct s* p = new struct s; // global s
|
| 127 |
p->a = s; // parameter s
|
| 128 |
}
|
| 129 |
```
|
| 130 |
|
| 131 |
+
— *end example*]
|
| 132 |
+
|
| 133 |
+
[*Note 3*:
|
| 134 |
+
|
| 135 |
The declaration of a class name takes effect immediately after the
|
| 136 |
*identifier* is seen in the class definition or
|
| 137 |
*elaborated-type-specifier*. For example,
|
| 138 |
|
| 139 |
``` cpp
|
|
|
|
| 143 |
first specifies `A` to be the name of a class and then redefines it as
|
| 144 |
the name of a pointer to an object of that class. This means that the
|
| 145 |
elaborated form `class` `A` must be used to refer to the class. Such
|
| 146 |
artistry with names can be confusing and is best avoided.
|
| 147 |
|
| 148 |
+
— *end note*]
|
| 149 |
+
|
| 150 |
A *typedef-name* ([[dcl.typedef]]) that names a class type, or a
|
| 151 |
cv-qualified version thereof, is also a *class-name*. If a
|
| 152 |
*typedef-name* that names a cv-qualified class type is used where a
|
| 153 |
*class-name* is required, the cv-qualifiers are ignored. A
|
| 154 |
*typedef-name* shall not be used as the *identifier* in a *class-head*.
|