首页 > 其他 > 详细

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

时间:2017-08-10 10:02:22      阅读:257      评论:0      收藏:0      [点我收藏+]

const限定符可以将一个对象转换为一个常量。因为常量在定义后就不能被修改,所以定义时必须初始化。

在全局作用域里定义非const变量时,它在整个程序中都可以访问。例如:

//file1.cpp
int counter;   //definitaion

//file2.cpp
extern int counter;   //use counter in file1

与其他变量不同,除非特别说明,在全局作用域声明的const变量时定义该对象的文件的局部变量。此变量只能在那个文件中,不能被其他文件访问。

通过指定const变量为extern,就可以在整个程序中访问const对象。

//file1.cpp
extern const int bufsize = fcn();   //define and initialize a const that is accessible to other files

//file2.cpp
extern const int bufsize;   //use bufsize in file1

非const变量默认为extern。要使const变量能够在其他文件中访问,必须显式地指定它为extern。

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

原文:http://www.cnblogs.com/cinvzi/p/7337350.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!