const 对象默认为文件的局部变量

定义非const变量时候,他是可以被其它文件访问的,(不用引入哪个文件)只需在使用的地方声明下,这个变量来之外部extern Type_Name Variable_Name。而const变量不能这样,默认的是文件的局部变量。若要改变这种情况则需特别声明  extern const int a;


cc.h

#ifndef TEST
#define TEST

	extern const int a = 10;						//如果a不声明为extern,则不能在test.cpp中不通过引入文件就访问不到他。
	int b=100;

#endif

test.cpp

#include <iostream>
//#include "cc.h"
using namespace std;

int main()
{
	extern int b;
	extern const int a;
	cout << a +10<< endl;
	cout << b +100<< endl;
	return 0;
}

vs2010命令行:

cl -c cc.h

cl test.cpp cc.obj

我们就看出来extern的效果了。。。



posted @ 2012-11-25 10:18  N3verL4nd  阅读(138)  评论(0编辑  收藏  举报