定义非const变量时候,他是可以被其它文件访问的,(不用引入哪个文件)只需在使用的地方声明下,这个变量来之外部extern Type_Name Variable_Name。而const变量不能这样,默认的是文件的局部变量。若要改变这种情况则需特别声明 extern cconst int a;
/*
* const.cpp
*
* Created on: Nov 3, 2011
* Author: ubuntu
*/
extern const int a = 78;
//如果a不声明为extern,则不能在test.cpp中不通过引入文件就访问不到他。
int b=99;
#include<iostream>
//#include"const.cpp"
using namespace std;
int main()
{
extern int b;
extern const int a;
cout << a +9<< endl;
cout << b +89<< endl;
return 0;
}
本文探讨了C++中const与非const变量的区别,特别是在不同文件间访问这些变量时的行为差异。通过具体代码示例展示了如何正确声明和使用这两种类型的变量。

1307

被折叠的 条评论
为什么被折叠?



