Use of the typedef specifier with class types is supported largely because of the ANSI C practice of declaring unnamed structures in typedef declarations. For example, many C programmers use the following:
typedef struct // Declare an unnamed structure and give it the
{ // typedef name POINT.
unsigned x;
unsigned y;
} POINT;
The advantage of such a declaration is that it enables declarations like:
POINT ptOrigin;
instead of:
struct point_t ptOrigin;
In C++, the difference between typedef names and real types (declared with the class, struct, union, and enum keywords) is more distinct. Although the C practice of declaring a nameless structure in a typedef statement still works, it provides no notational benefits as it does in C.
In the following code, the POINT
function is not a type constructor. It is interpreted as a function declarator with an int return type.
typedef struct
{
POINT(); // Not a constructor.
unsigned x;
unsigned y;
} POINT;
The preceding example declares a class named POINT
using the unnamed class typedef syntax. POINT
is treated as a class name; however, the following restrictions apply to names introduced this way:
In summary, this syntax does not provide any mechanism for inheritance, construction, or destruction.