共用方式為


Anonymous Class Types 

Classes can be anonymous — that is, they can be declared without an identifier. This is useful when you replace a class name with a typedef name, as in the following:

typedef struct
{
    unsigned x;
    unsigned y;
} POINT;

Note

The use of anonymous classes shown in the previous example is useful for preserving compatibility with existing C code. In some C code, the use of typedef in conjunction with anonymous structures is prevalent.

Anonymous classes are also useful when you want a reference to a class member to appear as though it were not contained in a separate class, as in the following:

struct PTValue
{
    POINT ptLoc;
    union
    {
        int  iValue;
        long lValue;
    };
};

PTValue ptv;

In the preceding code, iValue can be accessed using the object member-selection operator (.) as follows:

int i = ptv.iValue;

Anonymous classes are subject to certain restrictions. (For more information about anonymous unions, see Unions.) Anonymous classes:

  • Cannot have a constructor or destructor.

  • Cannot be passed as arguments to functions (unless type checking is defeated using ellipses).

  • Cannot be returned as return values from functions.

See Also

Reference

Defining Class Types