C++ 之 const references
extraction from The C++ Programming Language 4th. ed., Section 7.7 References, Bjarne Stroustrup
To reflect the lvalue/rvalue and const/non-const distinctions, there are three kinds of references:
- lvalue references: to refer to objects whose value we want to change
- const references: to refer to objects whose value we do not want to change (e.g., a constant)
- rvalue references: to refer to objects whose value we do not need to preserve after we have used it (e.g., a temporary)
Collectively, they are called referencs. The first two are both called lvalue references.
The obvious implementation of a reference is as a (constant) pointer that is dereferenced each time it is used. It doesn't do much harm to think about references that way, as long as one remembers that a reference isn't an object that can be manipulated the way a pointer is.
In some cases, the compiler can optimize away a reference so that there is no object representing that reference at run time.
Intialization of a reference is trivial when the initializer is an lvalue (an object whose address you can take). The initializer for a "plain" T& must be an lvaue of type T.
The intializer for a const T& need not be an lvaue or even of type T. In such cases:
- First, implicit type conversion to T is applied if necessary.
- Then, the resulting value is placed in a temporary variable of type T.
- Finally, this temporary variable is used as the value of the initializer.
Consider:
double &dr=1; //error: lvalue needed
const double& cdr{1}; //OK
The iterpretation of this last initialization might be:
double temp = double{1};
cosnt double &cdr {temp};
A temporary created to hold a reference initializer persists until the end of its reference's scope.
References to variables and references to constants are distinguished because introducing a temporary for a variable would have been highly error-prone; an assignment to the variable would become an assignment to the -- soon-to-disappear -- temporary. No such problem exists for references to constants, and references to constants are often important as function arguments.
C++ 之 const references的更多相关文章
- References & the Copy-Constructor
1 There are certain rules when using references: (Page 451) A reference must be initialized when it ...
- const引用返回值
一.引用 引用是别名 必须在定义引用时进行初始化.初始化是指明引用指向哪个对象的唯一方法. const 引用是指向 const 对象的引用: ; const int &refVal = iva ...
- 非const引用不能指向临时变量
没找到具体原因,MSDN看到下面这句,VC是从2008才有这一限制的,感觉就是从语法上对临时变量增加了限定,因为一般说来修改一个临时变量是毫无意义的,通过增加限定,强调临时变量只读语义.虽然实际上修改 ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- 总结c++ primer中的notes
转载:http://blog.csdn.net/ace_fei/article/details/7386517 说明: C++ Primer, Fourth Edition (中英文)下载地址:htt ...
- C++: Why pass-by-value is generally more efficient than pass-by-reference for built-in (i.e., C-like) types
A compiler vendor would typically implement a reference as a pointer. Pointers tend to be the same s ...
- 阅读Google的C++代码规范有感
李开复曾在微博上说过,Google的C++代码规范是全球最好的一份C++代码规范,没有之一.最近花了点时间看了下这份代码规范,收获确实很大,在编程过程中一些乱七八糟的坏习惯也该改一改了.最新的英文版见 ...
- (转) Functions
Functions Functions allow to structure programs in segments of code to perform individual tasks. In ...
- openssl 1.1.1 reference
openssl 1.1.1 include/openssl aes.h: # define HEADER_AES_H aes.h: # define AES_ENCRYPT 1 aes.h: # de ...
随机推荐
- Android -- 桌面悬浮,QQ管家火箭实现
续上一篇博客<Android -- 桌面悬浮,仿360>,传送门:http://www.cnblogs.com/yydcdut/p/3909888.html,在此代码上继续添加实现. 比起 ...
- jquery 实现邮箱输入自动提示功能
邮箱的广泛使用得益于它的免费,因此很多网站在注册的时候都会直接使用邮箱作为账号名 为了提高用户的体验,很多网站都会实现邮箱输入的自动提示功能,所有自己也实现了一个,先看下效果吧,觉得效果还行的就拿去 ...
- VS条件断点的一个坑
今天早上要调试一个方法,所以在方法的入口处也就是’{’打了一个条件断点,可是随后就发现对这个方法的调用都无法获得正确的结果了,不管传递什么参数,方法的返回值都一样,而且这个返回值与输入的参数完全无关, ...
- js的浅拷贝和深拷贝
转载:http://www.jb51.net/article/91906.htm //深拷贝,并且把合并后的值放到第二个参数里 function deepCopy(p, c) { var c = c ...
- SQLite剖析之体系结构
1.通过官方的SQLite架构文档,理清大体的系统层次:Architecture of SQLite 2.阅读SQLite Documentation中Technical/Design Documen ...
- 彻底明白IP地址——IP地址的介绍
彻底明白IP地址——IP地址的介绍 [ 作者:担子 转贴自:赛迪网 点击数:9692 更新时间:2004-12-22 ] IP地址的介绍 1.IP地址的表示方法 IP地址 = ...
- JavaScript学习笔记-商品管理新增/删除/修改功能
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- 简易版viewport
首先这里涉及到虚拟像素和物理像素,不管是移动设备还是pc都存在这个概念1.在显示器精度还不是很高的年代,一个物理像素就等于一个虚拟像素,1000px的网页就占据了1000的物理像素(如果你玩过小霸王, ...
- Linux下sysstat工具学习
Linux下,我们多用ssh链接服务器远程操控.对于系统的监控必不可少,sysstat很不错的监控工具包. sysstat官网:http://sebastien.godard.pagesperso-o ...
- Windows10一周年庆典壁纸
example: 下载:http://pan.baidu.com/s/1b55D5k