Team LiB
Previous Section Next Section

2.6. Typedef Names

2.6. typedef 名字

A typedef lets us define a synonym for a type:

typedef 可以用来定义类型的同义词:

     typedef double wages;       //  wages is a synonym for double
     typedef int exam_score;     //  exam_score is a synonym for int
     typedef wages salary;       //  indirect synonym for double

A typedef name can be used as a type specifier:

typedef 名字可以用作类型说明符:

     wages hourly, weekly;     // double hourly, weekly;
     exam_score test_result;   // int test_result;

A typedef definition begins with the keyword typedef, followed by the data type and identifier. The identifier, or typedef name, does not introduce a new type but rather a synonym for the existing data type. A typedef name can appear anywhere in a program that a type name can appear.

typedef 定义以关键字 typedef 开始,后面是数据类型和标识符。标识符或类型名并没有引入新的类型,而只是现有数据类型的同义词。typedef 名字可出现在程序中类型名可出现的任何位置。

Typedefs are commonly used for one of three purposes:

typedef 通常被用于以下三种目的:

  • To hide the implementation of a given type and emphasize instead the purpose for which the type is used

    为了隐藏特定类型的实现,强调使用类型的目的。

  • To streamline complex type definitions, making them easier to understand

    简化复杂的类型定义,使其更易理解。

  • To allow a single type to be used for more than one purpose while making the purpose clear each time the type is used

    允许一种类型用于多个目的,同时使得每次使用该类型的目的明确。

Team LiB
Previous Section Next Section