首页 > 其他 > 详细

References & the Copy-Constructor

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!