设为首页 加入收藏

TOP

2.4 const Qualifier (4)
2013-10-07 16:18:12 来源: 作者: 【 】 浏览:63
Tags:2.4 const Qualifier

2.4.3 Top-Level const

As we’ve seen, a pointer is an object that can point to a different object. As a result, we can talk independently about whether a pointer is const and whether the objects to which it can point are const. We use the termtop-level const to indicate that the pointer itself is a const. When a pointer can point to a const object, we refer to that const as a low-level const.

EXERCISES SECTION 2.4.2

Exercise 2.27: Which of the following initializations are legal Explain why.

(a) int i = -1, &r = 0; (b) int *const p2 = &i2;

(c) const int i = -1, &r = 0; (d) const int *const p3 = &i2;

(e) const int *p1 = &i2; (f) const int &const r2;

(g) const int i2 = i, &r = i;

Exercise 2.28: Explain the following definitions. Identify any that are illegal.

(a) int i, *const cp; (b) int *p1, *const p2;

(c) const int ic, &r = ic; (d) const int *const p3;

(e) const int *p;

Exercise 2.29: Uing the variables in the previous exercise, which of the following assignments are legal Explain why.

(a) i = ic; (b) p1 = p3;

(c) p1 = ⁣ (d) p3 = ⁣

(e) p2 = p1; (f) ic = *p3;

More generally, top-level const indicates that an object itself is const. Toplevel const can appear in any object type, i.e., one of the built-in arithmetic types, a class type, or a pointer type. Low-level const appears in the base type of compound types such as pointers or references. Note that pointer types, unlike most other types, can have both top-level and low-level const independently:

  1. int i = 0;   
  2. int *const p1 = &i; // we can’t change the value of p1; const is top-level   
  3. const int ci = 42; // we cannot change ci; const is top-level   
  4. const int *p2 = &ci; // we can change p2; const is low-level   
  5. const int *const p3 = p2; // right-most const is top-level, left-most is not   
  6. const int &r = ci; // const in reference types is always low-level  

The distinction between top-level and low-level matters when we copy an object. When we copy an object, top-level consts are ignored:

  1. i = ci; // ok: copying the value of ci; top-level const in ci is ignored   
  2. p2 = p3; // ok: pointed-to type matches; top-level const in p3 is ignored  

Copying an object doesn’t change the copied object. As a result, it is immaterial whether the object copied from or copied into is const.

On the other hand, low-level const is never ignored. When we copy an object, both objects must have the same low-level const qualification or there must be a conversion between the types of the two objects. In general, we can convert a nonconst to const but not the other way round:
 

  1. int *p = p3; // error: p3 has a low-level const but p doesn’t   
  2. p2 = p3; // ok: p2 has the same low-level const qualification as p3   
  3. p2 = &i; // ok: we can convert int* to const int*   
  4. int &r = ci; // error: can’t bind an ordinary int& to a const int object   
  5. const int &r2 = i; // ok: can bind const int& to plain int 

p3 has both a top-level and low-level const. When we copy p3, we can ignore its top-level const but not the fact that it points to a const type. Hence, we cannot use p3 to initialize p, which points to a plain (nonconst) int. On the other hand, we can assign p3 to p2. Both pointers have the same (low-level const) type. The fact that p3 is a const pointer (i.e., that it has a top-level const) doesn’t matter.

EXERCISES SECTION 2.4.3

Exercise 2.30: For each of the following declarations indicate whether the object being declared has top-level or low-level const.

  1. const int v2 = 0; int v1 = v2;   
  2. int *p1 = &v1, &r1 = v1;   
  3. const int *p2 = &v2, *const p3 = &i, &r2 = v2;  

Exercise 2.31: Given the declarations in the previous exercise determine whether the following assignments are legal. Explain how the top-level or low-level const applies in each case.

  1. r1 = v2;   
  2. p1 = p2; p2 = p1;   
  3. p1 = p3; p2 = p3;  

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

评论

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

·用 Python 进行数据 (2025-12-25 15:49:09)
·如何学习Python数据 (2025-12-25 15:49:07)
·利用Python进行数据 (2025-12-25 15:49:04)
·Java 学习线路图是怎 (2025-12-25 15:19:15)
·关于 Java 学习,有 (2025-12-25 15:19:12)