Qt chat1

 const* and *const
Suppose that we have a pointer ptr that is storing the address of a variable vbl:
Type* ptr = &vbl;
When using a pointer, two objects are involved: the pointer itself and the object
pointed to. This means there are two possible layers of protection that we might
want to impose with const:
1.13 CONST* AND *CONST
41
1. If we want to make sure that ptr cannot point to any other memory
location, we can write it one of two ways:
Type* const ptr = &vbl;
Type* const ptr(&vbl);
The pointer is a const but the addressed object can be changed.
2. If we want to make sure that the value of vbl cannot be changed by
dereferencing ptr, we can write it in two ways:
const Type* ptr = &vbl;
const Type* ptr(&vbl);
In this case, the addressed object is a constant but the pointer is not.
In addition, if we want to impose both kinds of protection we can write:
const Type* const ptr = &vbl;
const Type* const ptr(&vbl);
Here is a good way to remember which is which: Read each of the
following definitions from right to left (starting with the defined variable).
const char * x = &p;
/* x is a pointer to const char*/
char * const y = &q;
/* y is a const pointer to char */
const char * const z = &r; /* z is a const pointer to a const char */
A short program that demonstrates the two kinds of protection is shown
in Example 1.22.
E X A M P L E 1 . 2 2 src/constptr/constptr.cpp
#include <iostream>
using namespace std;
int main() {
int m1(11), m2(13);
const int* n1(&m1);
int* const n2(&m2);
// First snapshot
cout << "n1 = " << n1 << '\t' << *n1 << '\n'
<< "n2 = " << n2 << '\t' << *n2 << endl;
n1 = &m2;
1
//*n1 = 15;
2
m1 = 17;
continued
CHAPTER 1: C++ INTRODUCTION
42
3
//n2 = &m1;
4
*n2 = 16;
// Second snapshot
cout << "n1 = " << n1 << '\t' << *n1 << '\n'
<< "n2 = " << n2 << '\t' << *n2 << endl;
return 0;
}
Output:
src/constptr> g++ constptr.cpp
src/constptr> ./a.out
n1 = 0xbffff504 11
n2 = 0xbffff500 13
n1 = 0xbffff500 16
n2 = 0xbffff500 16
src/constptr>
1
2
3
4
error: assignment of read-only location
m2 is an ordinary int variable—OK to assign
error: assignment of read-only variable ‘n2’
okay to change target
Figure 1.2 shows two snapshots of memory that may help to clarify what is hap-
pening when the program runs. Notice that the program produces a memory leak.
First Snapshot
const (via n1)
0xbffff504
n1
11
m1
location 0xbffff504
const
0xbffff500
n2
13
m2
location 0xbffff500
Second Snapshot
const (via n1)
0xbffff504
n1
17
m1
location 0xbffff504
const
0xbffff500
n2
16
m2
location 0xbffff500
F I G U R E 1 . 2 Two snapshots of memory showing what the program
in Example 1.2 produces
1 . 1 4 R E F E R E N C E VA R I A B L E S
43
An object that is read-only when accessed through one pointer may be changeable
when accessed through another pointer. This fact is commonly exploited in the
design of functions.
char* strcpy(char* dst, const char* src); // strcpy cannot change *src
It is okay to assign the address of a variable to a pointer to const. It is an error to
assign the address of a const object to an unrestricted (i.e., non-const) pointer
variable because that would allow the const object’s value to be changed.
int a = 1;
const int c = 2;
const int* p1 = &c;
const int* p2 = &a;
int* p3 = &c;
*p3 = 5;
//
//
//
//
OK
OK
error
error
It is good programming practice to use const to protect pointer and reference
parameters that do not need to be altered by the action of a function. Read-only
reference parameters provide the power and efficiency of pass-by-reference with
the safety of pass-by-value (see Section 5.6).


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值