Const和#define

C++中的const類似於宏定義

Const int c = 5 ≈#define c 5

C++中的常量與宏定義不同

Const常量有編譯器處理,提供類型檢查作用域檢查

#define預處理器處理,只是單純的文本替換

例:

Void fun1()

{

#define a 10

Const int b = 20;

//#undef //取消該句以後的作用域

}

Void fun2()

{

Printf(“%d”,a);

Printf(“%d”,b);

}

Void main()

{

Fun1();

Fun2();

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章