Typedef declaration
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
typedef声明提供了一种方法,可以在任何地方使用的地方(可能是复杂的)类型名称创建一个别名.
原文:
The typedef declaration provides a way to create an alias that can be used anywhere in place of a (possibly complex) type name.
目录 |
[编辑] 语法
typedef type_declaration;
|
|||||||||
[编辑] 解释
声明如下关键字typedef否则一个正常的类型声明,惟其他类型说明符,例如static,不能使用。在同一行上(如int和指针为int),它可能会声明一个或许多indentifiers中,它可能会声明数组和成为一个typedef名称,而每个标识符在这个声明引入的函数类型,指针和引用,类类型,等等。比一个对象,它会成为如果关键字typedef除去.
原文:
The declaration that follows the keyword typedef is otherwise a normal type declaration (except that other type specifiers, e.g. static, cannot be used). It may declare one or many indentifiers on the same line (e.g. int and a pointer to int), it may declare array and function types, pointers and references, class types, etc. Every identifier introduced in this declaration becomes a typedef-name rather than an object that it would become if the keyword typedef was removed.
typedef定义的名称是为现有类型的别名,而不是新型的声明。 typedef不能用来改变现有的类型名称(包括一个typedef名称)的意义。声明后,一个typedef名字可能会被重新声明,指相同类型的。 typedef名称是唯一的有效的范围内,他们是可见:不同的函数或类声明可以定义相同名称的类型与不同的含义.....
原文:
The typedef-names are aliases for existing types, and are not declarations of new types. Typedef cannot be used to change the meaning of an existing type name (including a typedef-name). Once declared, a typedef-name may only be redeclared to refer to the same type again. Typedef names are only in effect in the scope where they are visible: different functions or class declarations may define identically-named types with different meaning.
[编辑] 关键字
[编辑] 示例
// simple typedef typedef unsigned long ulong; // the following two objects have the same type unsigned long l1; ulong l2; // more complicated typedef typedef int int_t, *intp_t, (&fp)(int, ulong), arr_t[10]; // the following two objects have the same type int a1[10]; arr_t a2; // common C idiom to avoid having to write "struct S" typedef struct {int a; int b;} S; // the following two objects have the same type struct {int a; int b;} s1; S s2; // error: conflicting type specifier // typedef static unsigned int uint; // std::add_const, like many other metafunctions, use member typedefs template< class T> struct add_const { typedef const T type; };