詳解C++中typedef 和 #define 的區(qū)別
1、執(zhí)行上不同
關(guān)鍵字 typedef 在編譯階段有效,由于是在編譯階段,因此 typedef 有類型檢查的功能。
#define 則是宏定義,發(fā)生在預(yù)處理階段,也就是編譯之前,它只進行簡單而機械的字符串替換,而不進行任何檢查。
例如:typedef 會做相應(yīng)的類型檢查
typedef unsigned int UINT; void func() { UINT value = "abc"; // error C2440: 'initializing' : cannot convert from 'const char [4]' to 'UINT',會編譯不通過 cout << value << endl; }
#define不做類型檢查:
// #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; }
程序的輸出結(jié)果是: 36,根本原因就在于 #define 只是簡單的字符串替換。
2、功能有差異
typedef 用來定義類型的別名,定義與平臺無關(guān)的數(shù)據(jù)類型,與 struct 的結(jié)合使用等。
比如:定義一個叫 FALSE 的浮點類型,在目標(biāo)平臺一上,讓它表示最高精度的類型為:
typedef long double FALSE;
在不支持 long double 的平臺二上,改為:
typedef double FALSE;
在連 double 都不支持的平臺三上,改為:
typedef float FALSE;
也就是說,當(dāng)跨平臺時,只要改下 typedef 本身就行,不用對其他源碼做任何修改。
#define 不只是可以為類型取別名,還可以定義常量、變量、編譯開關(guān)等。
3、作用域不同
#define 沒有作用域的限制,只要是之前預(yù)定義過的宏,在以后的程序中都可以使用。
而 typedef 有自己的作用域。
例如:沒有作用域的限制,只要是之前預(yù)定義過就可以
void func1() { #define HW "HelloWorld"; } void func2() { string str = HW; cout << str << endl; }
而typedef有自己的作用域:
函數(shù):
void func1() { typedef unsigned int UINT; } void func2() { UINT uValue = 5;//error C2065: 'UINT' : undeclared identifier,在此函數(shù)中未定義 }
類:
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定義的類型別名還具有相應(yīng)的訪問權(quán)限:
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' }
默認(rèn)的typedef為私有,而給UINT加上public訪問權(quán)限后,則可編譯通過。
class A { public: typedef unsigned int UINT; UINT valueA; A() : valueA(0){} }; void func3() { A::UINT i = 1; cout << i << endl; }
4、對指針的操作
二者修飾指針類型時,作用不同。
typedef int * pint; #define PINT int * int i1 = 1, i2 = 2; const pint p1 = &i1; //p不可更改,p指向的內(nèi)容可以更改,相當(dāng)于 int * const p; const PINT p2 = &i2; //p可以更改,p指向的內(nèi)容不能更改,相當(dāng)于 const int *p;或 int const *p; pint s1, s2; //s1和s2都是int型指針 PINT s3, s4; //相當(dāng)于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; }
結(jié)果:
p1:00EFD094 *p1:1
p1:00EFD094 *p1:5
p2:00EFD098 *p2:2
p2:00EFD094 *p2:5
轉(zhuǎn)載于
到此這篇關(guān)于詳解C++中typedef 與 #define 的區(qū)別的文章就介紹到這了,更多相關(guān)c++ typedef 與 #define區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++?JSON庫?nlohmann::basic_json::accept的用法解析
nlohmann::basic_json::accept 是 Nlohmann JSON 庫中的一個方法,它用于檢查一個字符串是否可以解析為有效的 JSON,這篇文章主要介紹了C++?JSON庫nlohmann::basic_json::accept的用法,需要的朋友可以參考下2023-06-06C++實現(xiàn)簡單的學(xué)生成績管理系統(tǒng)
這篇文章主要為大家詳細介紹了C++實現(xiàn)簡單的學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03