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();

}

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