c++中有一个关键字typedef,它主要是把一种数据类型定义为另一个标识符来使用,在程序中使用该标识符来实现相应数据类型变量的定义。下面给出三种常用的地方。
(1)简单类型替换:
#include "stdafx.h"#include <iostream> #include <string>using namespace std;typedef int INT;
int main( ){INT a;a = 10;//a = "a";//falsecout<<a;}
(2)定义数组类型:
#include "stdafx.h" #include <iostream> #include <string> using namespace std; typedef int A[3]; int main(){ A b = {3,4,5}; cout<<sizeof(b); }
(3)定义函数指针
#include "stdafx.h"#include <iostream> #include <string>using namespace std;
typedef void (*F)(int);void print1(int x){cout<<x;}int main(){F a;a = print1;(*a)(20);}
从上面的例子可以看出,有时typedef可以简化操作符,或使用自己熟悉的操作符来代替不熟悉的。
本文源自:翔宇亭——IT乐园(http://),转载请保留此信息!