1、執行上不同
關鍵字 typedef 在編譯階段有效,由于是在編譯階段,因此 typedef 有類型檢查的功能。
#define 則是宏定義,發生在預處理階段,也就是編譯之前,它只進行簡單而機械的字符串替換,而不進行任何檢查。
例如:typedef 會做相應的類型檢查
1
2
3
4
5
6
7
|
typedef unsigned int UINT ; void func() { UINT value = "abc" ; // error C2440: 'initializing' : cannot convert from 'const char [4]' to 'UINT',會編譯不通過 cout << value << endl; } |
#define不做類型檢查:
1
2
3
4
5
6
7
8
9
|
// #define用法例子: #define f(x) x*x int main() { int a=6, b=2, c; c=f(a) / f(b); printf ( "%d\n" , c); return 0; } |
程序的輸出結果是: 36,根本原因就在于 #define 只是簡單的字符串替換。
2、功能有差異
typedef 用來定義類型的別名,定義與平臺無關的數據類型,與 struct 的結合使用等。
比如:定義一個叫 FALSE 的浮點類型,在目標平臺一上,讓它表示最高精度的類型為:
1
|
typedef long double FALSE; |
在不支持 long double 的平臺二上,改為:
1
|
typedef double FALSE; |
在連 double 都不支持的平臺三上,改為:
1
|
typedef float FALSE; |
也就是說,當跨平臺時,只要改下 typedef 本身就行,不用對其他源碼做任何修改。
#define 不只是可以為類型取別名,還可以定義常量、變量、編譯開關等。
3、作用域不同
#define 沒有作用域的限制,只要是之前預定義過的宏,在以后的程序中都可以使用。
而 typedef 有自己的作用域。
例如:沒有作用域的限制,只要是之前預定義過就可以
1
2
3
4
5
6
7
8
9
10
|
void func1() { #define HW "HelloWorld"; } void func2() { string str = HW; cout << str << endl; } |
而typedef有自己的作用域:
函數:
1
2
3
4
5
6
7
8
9
|
void func1() { typedef unsigned int UINT ; } void func2() { UINT uValue = 5; //error C2065: 'UINT' : undeclared identifier,在此函數中未定義 } |
類:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class A { typedef unsigned int UINT ; UINT valueA; A() : valueA(0){} }; class B { UINT valueB; //error C2146: syntax error : missing ';' before identifier 'valueB' //error C4430: missing type specifier - int assumed. Note: C++ does not support default-int }; |
上面例子在B類中使用UINT會出錯,因為UINT只在類A的作用域中。
此外,在類中用typedef定義的類型別名還具有相應的訪問權限:
1
2
3
4
5
6
7
8
9
10
11
12
|
class A { typedef unsigned int UINT ; UINT valueA; A() : valueA(0){} }; void func3() { A:: UINT i = 1; // error C2248: 'A::UINT' : cannot access private typedef declared in class 'A' } |
默認的typedef為私有,而給UINT加上public訪問權限后,則可編譯通過。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class A { public : typedef unsigned int UINT ; UINT valueA; A() : valueA(0){} }; void func3() { A:: UINT i = 1; cout << i << endl; } |
4、對指針的操作
二者修飾指針類型時,作用不同。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
typedef int * pint; #define PINT int * int i1 = 1, i2 = 2; const pint p1 = &i1; //p不可更改,p指向的內容可以更改,相當于 int * const p; const PINT p2 = &i2; //p可以更改,p指向的內容不能更改,相當于 const int *p;或 int const *p; pint s1, s2; //s1和s2都是int型指針 PINT s3, s4; //相當于int * s3,s4;只有一個是指針。 void TestPointer() { cout << "p1:" << p1 << " *p1:" << *p1 << endl; //p1 = &i2; //error C3892: 'p1' : you cannot assign to a variable that is const *p1 = 5; cout << "p1:" << p1 << " *p1:" << *p1 << endl; cout << "p2:" << p2 << " *p2:" << *p2 << endl; //*p2 = 10; //error C3892: 'p2' : you cannot assign to a variable that is const p2 = &i1; cout << "p2:" << p2 << " *p2:" << *p2 << endl; } |
結果:
p1:00EFD094 *p1:1
p1:00EFD094 *p1:5
p2:00EFD098 *p2:2
p2:00EFD094 *p2:5
轉載于
1、C++數據類型
到此這篇關于詳解C++中typedef 與 #define 的區別的文章就介紹到這了,更多相關c++ typedef 與 #define區別內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/pam-sh/p/15232940.html