Defining Class Types

Class types are defined using class-specifiers. Class types can be declared using elaborated-type-specifiers as shown in “Type Specifiers” in Chapter 6, on topic .

Syntax

class-specifier:
class-head
{ member-listopt }

class-head:
class-key imodelopt identifieroptbase-specopt
class-key imodelopt class-nameoptbase-specopt

class-key:
class
struct
union

imodel:
__near
__far
__export

Class names are introduced as identifiers immediately after the compiler processes them (before entry into the class body); they can be used to declare class members. This allows declaration of self-referential data structures, such as the following:

class Tree

{

public:

void *Data;

Tree *Left;

Tree *Right;

};

Structures, Classes, and Unions

The three class types are structure, class, and union. They are declared using the struct, class, and union keywords (see class-key syntax above). Table 8.1 shows differences among the three class types.

Table 8.1 Access Control and Constraints of Structures, Classesand Unions

Structures Classes Unions

class-key is struct class-key is class class-key is union
Default access is public Default access is private Default access is public
No usage constraints No usage constraints Use only one member at a time

Anonymous Class Types

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

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 example:

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.

Point of Class Definition

A class is defined at the end of its class-specifier. Member functions need not be defined in order for the class to be considered defined. Consider the following example:

class Point // Point class

{ // considered defined.

public:

Point()

{ cx = cy = 0; } // Constructor defined.

Point( int x, int y )

{ cx = X, cy = Y; } // Constructor defined.

unsigned &x( unsigned ); // Accessor declared.

unsigned &y( unsigned ); // Accessor declared.

private:

unsigned cx, cy;

};

Even though the two accessor functions (x and y) are not yet defined, the class Point is considered defined. (Accessor functions are functions provided to give safe access to member data.)