const在C语言中的作用域是外部链接,如下图:
在main中查找外部的 const变量 int a = 10;在test.c文件中。是可以找到的。
也就是const修饰的变量在C中是外部链接的。
那么在C++中:
这样在main文件中是找不到test.cpp文件中的const int a = 10;
这个变量的。因为在C++中const变量是内部链接的。
如果非要这么做的话需要在test.cpp的const前面加上extern,
声明该变量为外部可见即可。
extern const int a = 10;