设为首页 加入收藏

TOP

2.4 const Qualifier (2)
2013-10-07 15:25:16 来源: 作者: 【 】 浏览:74
Tags:2.4 const Qualifier

Defining a const Object

There is still a serious problem with defining a variable to represent a constant value. The problem is that bufSize is modifiable. It is possible for bufSize to be changed—accidentally or otherwise. The const type qualifier provides a solution: It transforms an object into a constant.

  1. const int bufSize = 512; // input buffer size 

defines bufSize to be a constant initialized with the value 512. The variable bufSize is still an lvalue (Section 2.3.1, p. 45), but now the lvalue is unmodifiable. Any attempt to write to bufSize results in a compile-time error.

  1. bufSize = 0; // error: attempt to write to const object 

Becausewe cannot subsequently change the value of an object declared to be const, we must initialize it when it is defined:

  1. const std::string hi = "hello!"// ok: initialized  
  2. const int i, j = 0; // error: i is uninitialized const 

const Objects Are Local to a File By Default

When we define a nonconst variable at global scope (Section 2.3.6, p. 54), it is accessible throughout the program. We can define a nonconst variable in one file and—assuming an appropriate declaration has been made—can use that variable in another file:

  1. // file_1.cc  
  2. int counter; // definition  
  3. // file_2.cc  
  4. extern int counter; // uses counter fromfile_1  
  5. ++counter; // increments counter defined in file_1 

Unlike other variables, unless otherwise specified, const variables declared at global scope are local to the file in which the object is defined. The variable exists in that file only and cannot be accessed by other files.

We can make a const object accessible throughout the program by specifying that it is extern:

  1. // file_1.cc  
  2. // defines and initializes a const that is accessible to other files  
  3. extern const int bufSize = fcn();  
  4. // file_2.cc  
  5. extern const int bufSize; // uses bufSize fromfile_1  
  6. // uses bufSize defined in file_1  
  7. for (int index = 0; index != bufSize; ++index)  
  8. // ... 

注意:这里把 extern 和const 连用,定义了一个外部可见的const 对象。这个用法与英文原版书第53 页的讲法相呼应。一般我们把const 对象定义在头文件中,有了extern const 这种用法,我们也可以把const对象放在.cc 文件中,这样修改其值时就不必重新编译全部源文件了。

In this program, file_1.cc defines and initializes bufSize to the result returned from calling the function named fcn. The definition of bufSize is extern,meaning that bufSize can be used in other files. The declaration in file_2.cc is also made extern. In this case, the extern signifies that bufSize is a declaration and hence no initializer is provided.

We’ll see in Section 2.9.1 (p. 69) why const objects are made local to a file.

Nonconst variables are extern by default. To make a const variable accessible to other files we must explicitly specify that it is extern.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.4 const Qualifier (3) 下一篇2.4 const Qualifier (1)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C++中智能指针的性能 (2025-12-25 03:49:29)
·如何用智能指针实现c (2025-12-25 03:49:27)
·如何在 C 语言中管理 (2025-12-25 03:20:14)
·C语言和内存管理有什 (2025-12-25 03:20:11)
·为什么C语言从不被淘 (2025-12-25 03:20:08)