References

References and pointers have an interesting relationship — a reference acts like a const pointer that is implicitly dereferenced. Thus given the following:

1
2
3
int nValue = 5;
int * const pnValue = &nValue;
int &rnValue = nValue;

*pnValue and rnValue evaluate identically. As a result, the following two statements produce the same effect:

1
2
*pnValue = 6;
rnValue = 6;

Similarly, a const reference acts just like a const pointer to a const object that is implicitly dereferenced.

Because references always “point” to valid objects, and can never be pointed to deallocated memory, references are safer to use than pointers. If a task can be solved with either a reference or a pointer, the reference should generally be preferred. Pointers should generally only be used in situations where references are not sufficient (such as dynamically allocating memory).

Member selection

It is common to have either a pointer or a reference to a struct (or class). As you learned previously, you can select the member of a struct using the member selection operator (.):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Something
{
     int nValue;
     float fValue;
};
 
// Member selection using actual struct variable
Something sSomething;
sSomething.nValue = 5;
 
// Member selection using reference to struct
Something &rsSomething = sSomething;
rsSomething.nValue = 5;
 
// Member selection using pointer to struct
Something *psSomething = &sSomething;
(*psSomething).nValue = 5;

Note that the pointer dereference must be enclosed in parenthesis, because the member selection operator has a higher precedence than the dereference operator.

Because the syntax for access to structs and class members through a pointer is awkward, C++ offers a second member selection operator (->) for doing member selection from pointers. The following two lines are equivalent:

1
2
(*psSomething).nValue = 5;
psSomething->nValue = 5;

This is not only easier to type, but is also much less prone to error because there are no precedence issues to worry about. Consequently, when doing member access through a pointer, always use the -> operator.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值