[class.name] (original) (raw)

11 Classes [class]

11.3 Class names [class.name]

A class definition introduces a new type.

[Example 1:

struct X { int a; };struct Y { int a; }; X a1; Y a2;int a3;declares three variables of three different types.

This implies thata1 = a2; a1 = a3; are type mismatches, and thatint f(X);int f(Y); declare overloads ([over]) named f and not simply a single function f twice.

For the same reason,struct S { int a; };struct S { int a; }; is ill-formed because it defines S twice.

— _end example_]

[Note 1:

It can be necessary to use an elaborated-type-specifierto refer to a class that belongs to a scope in which its name is also bound to a variable, function, or enumerator ([basic.lookup.elab]).

[Example 2: struct stat { }; stat gstat; int stat(struct stat*); void f() { struct stat* ps; stat(ps); } — _end example_]

[Example 3: struct s { int a; };void g() { struct s; s* p; struct s { char* p; }; struct s; } — _end example_]

Such declarations allow definition of classes that refer to each other.

[Example 4: class Vector;class Matrix { friend Vector operator*(const Matrix&, const Vector&);};class Vector { friend Vector operator*(const Matrix&, const Vector&);};

— _end example_]

— _end note_]

[Note 2:

It differs from a class declaration in that it can refer to an existing class of the given name.

— _end note_]

[Example 5: struct s { int a; };void g(int s) { struct s* p = new struct s; p->a = s; } — _end example_]

[Note 3:

[Example 6:

class A * A;first specifies A to be the name of a class and then redefines it as the name of a pointer to an object of that class.

This means that the elaborated form class A must be used to refer to the class.

Such artistry with names can be confusing and is best avoided.

— _end example_]

— _end note_]