1、const和define的总结
![](https://img-blog.csdnimg.cn/20200410225646662.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpYW5naGVfd29yaw==,size_16,color_FFFFFF,t_70)
2、宏没有类型 const有
#define MAX 1024
const short my_max =1024;
void func(short i)
{
cout<<"short函数"<<endl;
}
void func(int i)
{
cout<<"int函数"<<endl;
}
void test05()
{
func(MAX);//int 函数
func(my_max);//short函数
}
3、宏的作用域是整个文件 const的作用域 以定义情况决定
void my_func(void)
{
//作用范围 是当前复合语句
const int my_num = 10;
//作用范围 当前位置 到文件结束
#define MY_NUM 10
}
void test06()
{
//cout<<"my_num = "<<my_num<<endl;//err 不识别
cout<<"MY_NUM = "<<MY_NUM<<endl;//ok 能识别
}
4、宏不能作为命名空间的成员 const可以
namespace A {
// const可以作为成员
const int my_a=100;
//MY_A 属于文件 不属于A
#define MY_A 200
}
void test07()
{
cout<<"my_a = "<<A::my_a<<endl;
//cout<<"MY_A = "<<A::MY_A<<endl;//err
cout<<"MY_A = "<<MY_A<<endl;
}