Export (0) Print
Expand All

Elaborated Type Specifiers

Elaborated type specifiers are used to declare user-defined types. These can be either class- or enumerated-types.

Grammar

elaborated-type-specifier :
class-key class-name
class-key identifier
enum-name
class-key :
class
struct
union

If identifier is specified, it is taken to be a class name. For example:

class Window;

This statement declares the Window identifier as a class name. This syntax is used for forward declaration of classes. For more information about class names, see Class Names.

If a name is declared using the union keyword, it must also be defined using the union keyword. Names that are defined using the class keyword can be declared using the struct keyword (and vice versa). Therefore, the following code samples are legal:

Example 1: Legal Type Specification

// elaborated_type_specifiers1.cpp
struct A;   // Forward declaration of A.

class A   // Define A.
{
public:
   int i;
};

int main()
{
}

Example 2: Legal Type Specification

// elaborated_type_specifiers2.cpp
class A;   // Forward declaration of A

struct A
{
private:
    int i;
};

int main()
{
}

Example 3: Legal Type Specification

// elaborated_type_specifiers3.cpp
union A;   // Forward declaration of A

union A
{
   int  i;
   char ch[2];
};

int main()
{
}

The following examples, however, are illegal:

Example 4: Illegal Type Specification

// elaborated_type_specifiers4.cpp
union A;   // Forward declaration of A.

struct A
{   // C2011
   int i;
};

Example 5: Illegal Type Specification

// elaborated_type_specifiers5.cpp
union A;   // Forward declaration of A.

class A
{   // C2011
public:
   int i;
};

Example 6: Illegal Type Specification

// elaborated_type_specifiers6.cpp
struct A;   // Forward declaration of A.

union A
{   // C2011
   int  i;
   char ch[2];
};

See Also

C++ Type Specifiers

Show:
© 2014 Microsoft