7.1.3 类型定义说明符+7.1.4 friend说明符

7.1.3 类型定义说明符

1、包含声明说名符typedef的声明声明了一个可在稍后用于命名基本(3.9.1)或复合(3.9.2类型的标识符。typedef说明符不可用于函数定义8.4),并且只能和类型说明符结合以构成decl-specifier-seq

typedef-name:

identifier

通过typedef说明符声明的名字为类型定义名。在声明的作用域内,类型定义名在句法上等价于一个关键字;并且如第8章所示,它命名了标识符所关联的类型。因而,类型定义名总是另一种类型的同义词。与类声明(9.1)和枚举声明不同,类型定义名并不引入新的类型。例如:在

typedef int MILES, *KLICKSP;

之后,

MILES distance;

extern KLICKSP metricp;

都是合法的声明;distanceint类型 metricp“pointer to int类型。

2、在非class作用域内,typedef说明符可重定义已声明的类型名字为指向其本身所属的类型。例如:

typedef struct s { /* ... */ } s;

typedef int I;

typedef int I;

typedef I I;

3、在给定的作用域中,typedef说明符不能重定义已声明的类型名为不同的类型。例如:

class complex { /* ... */ }; // 已定义类型名字complex

typedef int complex; // error: redefinition,不能重定义为不同的类型int

类似的,在给定的作用域内,不能声明类或枚举的名字为已经定义为其它类型的类型定义名。例如:

typedef int complex;

class complex { /* ... */ }; // error: redefinition

4、对类进行命名的类型定义名为类名9.1)。如果类型定义名跟在elaborated-type-specifier7.1.5.3)中的class-key后面,或位于类声明(9)的class-head中,或被用作构造函数和析构函数声明(12.1, 12.4)的声明符中的标识符,则程序是非法的。例如:

struct S {

S();

~S();

};

typedef struct S T;

S a = T(); // OK

struct T * p; // error

5、如果typedef声明定义了一个未具名的类(或枚举),出于链接目的,被声明为指向这个类类型(或枚举类型)的第一个类型定义名代表此类类型(或枚举类型)。例如:

typedef struct { } *ps, S; // S is the class name for linkage purposes

[注意:如果类型定义名被用在需要类名(或枚举名)的地方(比如构造函数和析构函数的声明),程序是非法的。例如:

typedef struct {

S(); //error: requires a return type because S is

// an ordinary member function, not a constructor

} S;

]

7.1.4 friend说明符

1friend说明符用于说明类成员的访问控制;参见11.4

posted @ 2006-03-27 16:28  Goncely  阅读(520)  评论(0编辑  收藏  举报