浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

A Quick Look at References and Constants

References and Constants

Table of Contents

  1. What is a reference?
  2. A class object (i.e. not a built-in object) may be returned by reference or passed by reference for better efficiency.
  3. When used properly, returning by reference allows the function to appear on the left-hand side of an expression.
  4. Passing/returning by reference allows the referenced object to be modified.
  5. If the intention of the class designer is to avoid modification of the passed/returned object, the reference should be made const.
  6. If a member function will not modify its data members, make the member function const.
  7. It is illegal to modify any object or reference that is declared const. Therefore, constant objects and references may only call constant member functions.

 


 

What is a reference?



A reference is nothing more than an alias. Anything you do with a
reference you are actually doing to the object you are referencing.

 

float x=3.14;
float& intref=x;     // intref is a reference to x
float* ptr=&intref;  // ptr holds the address of x

float y=2.58;
intref=y;            // x is now equal to 2.58

 

 

posted on 2012-04-14 09:34  lexus  阅读(173)  评论(0编辑  收藏  举报