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 ...
随机推荐
- WebApp:如何让安卓的webview缓存webapp的html、js和图片等资源
一.开发环境 客户端:安卓+webview(vuejs) 服务器端:tomcat 8.0 二.问题 使用安卓原生+web(基于webpack+vuejs)的方式开发了一个安卓应 ...
- 【Alpha版本】十天冲刺集结令
031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬涛 [Alp ...
- Web端PHP代码函数覆盖率测试解决方案
1. 关于代码覆盖率 衡量代码覆盖率有很多种层次,比如行覆盖率,函数/方法覆盖率,类覆盖率,分支覆盖率等等.代码覆盖率也是衡量测试质量的一个重要标准,对于黑盒测试来说,如果你不确定自己的测试用例是否真 ...
- 文本 To 音频
文本 To 音频 TextToSpeech介绍 TextToSpeech,简称 TTS,是Android 1.6版本中比较重要的新功能.将所指定的文本转成不同语言音频输出.它可以方便的嵌入到游戏或 ...
- shell note
1 输出重定向:ll > aaa 将输出内容 添加到aaa文件中 ll >> aaa将输出内容追加到aaa中 ll &>> abc 将输出内容不论正确或错误都保存 ...
- mysql找回密码
1.打开任务管理器,关闭mysqld.exe 2.win+r运行cmd打开控制台,输入mysqld --skip-grant-tables启动服务器 3.win+r重新运行cmd打开控制台,输入mys ...
- SpringMVC学习--springmvc和mybatis整合
简介 springMVC是表现层,service充当业务层,mybatis作为持久层,通过spring将这三层整合起来.如下图: 第一步:整合dao层 mybatis和spring整合,通过sprin ...
- Android-动画简介
Android中动画分为3种: ween Animation:通过对场景里的对象不断做图像变换(平移.缩放.旋转)产生动画效果,即是一种渐变动画: 也称View动画:也叫渐变动画,针对View的动画, ...
- angular指令大全
这篇文章的案例都是来自官方,引用的cdn来自bootcss, 因为angular的官方网站被屏了, 所以要翻, 不过我把整个文档下回来了,方便大家下载可以点击: 打开下载英文版 angular的指令 ...
- Maven简单介绍
Maven是基于项目对象模型,可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. Maven 除了以程序构建能力为特色之外,还提供高级项目管理工具.由于 Maven 的缺省构建规则 ...