Class names can be declared in global or class scope. If they are declared in class scope, they are referred to as “nested” classes.
Microsoft Specific
Function definitions are not permitted in local class declarations in Microsoft C++.
END Microsoft Specific
Any class name introduced in class scope hides other elements of the same name in an enclosing scope. Names hidden by such a declaration can then be referred to only by using an elaborated-type-specifier. The following example shows an example of using an elaborated-type-specifier to refer to a hidden name:
struct A // Global scope definition of A.
{
int a;
};
void main()
{
char A = 'a'; // Redefine the name A as an object.
struct A AObject;
...
}
Because the name A
that refers to the structure is hidden by the A
that refers to the char
object, struct (a class-key) must be used to declare AObject
as type A
.
You can use the class-key to declare a class without providing a definition. This nondefining declaration of a class introduces a class name for forward reference. This technique is useful when designing classes that refer to one another in friend declarations. It is also useful when class names must be present in header files but the definition is not required. For example:
// RECT.H
class Point; // Nondefining declaration of class Point.
class Line
{
public:
int Draw( Point &ptFrom, Point &ptTo );
...
};
In the preceding sample, the name Point
must be present, but it need not be a defining declaration that introduces the name.