-
基本概念
#define 只是在預處理過程對代碼進行簡單的替換。
typedef 只對已有的類型進行別名定義,不產生新的類型;其英文的解釋
Typedef does not work like typedef [type] [new name]. The [new name] part does not always come at the end
總結一下就是: 任何聲明變量的語句前面加上typedef之後,原來是變量的都變成一種類型。不管這個聲明中的標識符號出現在中間還是最後.
-
基本用法
//最基本的 typedef [type] [new name]
//int
typedef int INT;
INT a; //a是int類型
//結構體
typedef struct
{
int a;
int b;
} MyStruct;
MyStruct s_data;
//高級一點的 typedef
//指針
typedef int *p;//定義了一個名爲p的指針類型, 它指向int
//數組
typedef int INT[3];
INT a;
cout<<typeid(a).name()<<endl; //輸出的是 int[3]
//函數
typedef void (*pfun)(void); // pfun 類型是 void(*)(void)
pfun myFun; // 定義一個函數:void (*myFun)(void);
typedef int g(int); // 定義一個名爲g, 含一個int參數, 返回值爲int行的函數類型