C++ 语言
typedef
关键字
typedef
关键字
1. typedef
in C++
typedef
keyword is used to assign a new name to any existing data-type.
typedef
关键字用于为任何现有数据类型分配新名称。
For example, if we want to declare some variables of type unsigned int
, we have to write unsigned int
in a program and it can be quite hectic for some of us. So, we can assign a new name of our choice for unsigned int
using typedef
which can be used anytime we want to use unsigned int
in a program.
如果要声明一些类型为 unsigned int
的变量,则必须在程序中编写 unsigned int
。可以使用 typedef
为unsigned int
分配一个新的名称,该名称可以在我们想在程序中使用 unsigned int
的任何时候使用。
hectic [ˈhektɪk]:adj. 紧张忙碌的,肺病的,脸上发红的,狂热的 n. 脸红,患肺结核
Following is the syntax of typedef
(以下是 typedef
的语法)
typedef current_name new_name;
Now, suppose we want to declare two variables of type unsigned int
. Instead of writing unsigned int
again and again, let’s use a new name uint
in its place using typedef
as follows:
假设要声明两个类型为 unsigned int
的变量,让我们使用 typedef
这样的新名称 uint
如下:
typedef unsigned int uint;
uint i, j;
Now, we can write uint
in the whole program instead of unsigned int
. The above code is the same as writing:
现在可以在整个程序中编写 uint
而不是 unsigned int
。上面的代码与下面编写的相同:
unsigned int i, j;
//============================================================================
// Name : std::string::typedef
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
int main() {
typedef unsigned int ui;
ui i = 5, j = 8;
std::cout << "i = " << i << std::endl;
std::cout << "j = " << j << std::endl;
return 0;
}
i = 5
j = 8
请按任意键继续. . .
Thus, we can assign a new name to any data type.
我们可以为任何数据类型分配一个新名称。
Similarly, we can also use typedef
to assign a new name to structure which is a user-defined datatype as follows:
我们也可以使用 typedef
为结构分配一个新名称,该结构是用户定义的数据类型,如下所示:
typedef struct structure_name
{
data-type member-1;
data-type member-2;
data-type member-3;
data-type member-4;
} type_name;
特别注意:structure_name 和 type_name 概念不一样。
Now, while declaring variables of this structure type, we can write type_name
in place of struct structure_name
in the whole program.
在声明这种结构类型的变量时,我们可以在整个程序中写成 type_name
代替 struct structure_name
。
//============================================================================
// Name : std::string::typedef
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
#include <string>
using namespace std;
typedef struct student {
int index;
char name[30];
int phone_number;
} struct_type;
int main() {
struct_type p1, p2, p3;
p1.index = 1;
strcpy(p1.name, "Cheng");
p1.phone_number = 258;
p2.index = 2;
strcpy(p2.name, "Yong");
p2.phone_number = 369;
p3.index = 3;
strcpy(p3.name, "Qiang");
p3.phone_number = 147;
cout << "First Student" << endl;
cout << "index : " << p1.index << endl;
cout << "name : " << p1.name << endl;
cout << "phone number : " << p1.phone_number << endl;
cout << "\nSecond Student" << endl;
cout << "index : " << p2.index << endl;
cout << "name : " << p2.name << endl;
cout << "phone number : " << p2.phone_number << endl;
cout << "\nThird Student" << endl;
cout << "index : " << p3.index << endl;
cout << "name : " << p3.name << endl;
cout << "phone number : " << p3.phone_number << endl;
return 0;
}
First Student
index : 1
name : Cheng
phone number : 258
Second Student
index : 2
name : Yong
phone number : 369
Third Student
index : 3
name : Qiang
phone number : 147
请按任意键继续. . .
Here, the whole example is the same as we did in structure, the only difference is that we wrote struct_type
in place of struct student
i.e. we used the new type (named struct_type
) to declare the variables of this structure type (named student).
使用 struct_type
代替 struct student
,即使用了新的类型 (名为 struct_type
) 来声明此结构的变量类型 (命名学生)。
We can also use typedef
with union
. For this, everything will be same as that of the structure with the keyword union
in the place of struct
.
也可以将 typedef
和 union
一起使用,所有内容都与使用关键字 union
代替 struct
的结构相同。
2. typedef
关键字
//============================================================================
// Name : Yongqiang Cheng
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
int main()
{
typedef struct Books
{
char title[49];
char author[19];
int number;
} Books;
Books book1, book2;
return 0;
}
typedef
关键字定义非结构类型:
//============================================================================
// Name : Yongqiang Cheng
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
int main()
{
typedef int *pint32;
pint32 x, y;
int *m, n;
return 0;
}
x
、y
和 m
都是指向 int
的指针 (int *
),n
是 int
数据。
//============================================================================
// Name : Yongqiang Cheng
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
using std::string;
int main()
{
int yq = 9;
const int cyq = 6;
typedef int *pint;
const pint x(&yq), y(&yq);
return 0;
}