7.1.3 类型定义说明符+7.1.4 friend说明符
7.1.3 类型定义说明符
1、包含声明说名符typedef的声明声明了一个可在稍后用于命名基本(
typedef-name:
identifier
通过typedef说明符声明的名字为类型定义名。在声明的作用域内,类型定义名在句法上等价于一个关键字;并且如第8章所示,它命名了标识符所关联的类型。因而,类型定义名总是另一种类型的同义词。与类声明(9.1)和枚举声明不同,类型定义名并不引入新的类型。例如:在
typedef int MILES, *KLICKSP;
之后,
MILES distance;
extern KLICKSP metricp;
都是合法的声明;distance为int类型; 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-specifier(
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说明符
1、friend说明符用于说明类成员的访问控制;参见11.4。