c++ const

Const int bufSize=512//  bufSize 以后不能修改,定义时必须初始化

const std::string hi=“helloworld”

在全局作用域声明的 const 变量是定义该对象的文件的局部变量。此变量只存在于那个文件中,不能被其他文件访问。

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

// file_1.cc
// defines and initializes a const that is accessible to other files
extern const int bufSize = fcn();
// file_2.cc
extern const int bufSize; // uses bufSize from file_1
// uses bufSize defined in file_1
for (int index = 0; index != bufSize; ++index)

const 引用 ——是指向 const 对象的引用:

const int ival = 1024;

int &ref2 = ival;  //非法的

const int &refVal = ival; // ok: both reference and object are

 

 

posted on 2012-10-30 16:42  GIS-MAN  阅读(170)  评论(0编辑  收藏  举报

导航