码迷,mamicode.com
首页 > 其他好文 > 详细

References & the Copy-Constructor

时间:2014-06-25 21:07:06      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:des   com   strong   cti   for   line   

1 There are certain rules when using references:    (Page 451)

A reference must be initialized when it is created. (Pointers can be initialized at any time.)

Once a reference is initialized to an object, it cannot be changed to refer to another object.  (Pointers can be pointed to another object at any time.)

You cannot have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage.

 

2 References in functions     (Page 452)

When a reference is used as a function argument, any modification to the reference inside the function will cause changes to the argument outside the function.

 

3 Const references       (Page 453)

If you know the function will respect the constness of an object, making the arguement a const reference will allow the function to be used in all situations. For built-in types, the function will not modify the argument, and for user-defined types, the function will call only const member functions, and won‘t modify any public data members.

Temporary objects are always const, so if you donot use a const reference, that argument wonot be accepted by the compiler.

 

4 Argument-passing guidelines

The efficiency savings can be substantial for such a simple habit: to pass an argument by value requires a constructor and destructor call, but if you are not going to modify the argument then passing by const refecence only needs an address pushed on the stack.

References & the Copy-Constructor,布布扣,bubuko.com

References & the Copy-Constructor

标签:des   com   strong   cti   for   line   

原文地址:http://www.cnblogs.com/ruccsbingo/p/3805422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!