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 ...
随机推荐
- Caffe学习系列(7):solver及其配置
solver算是caffe的核心的核心,它协调着整个模型的运作.caffe程序运行必带的一个参数就是solver配置文件.运行代码一般为 # caffe train --solver=*_slover ...
- 【6年开源路】海王星给你好看!FineUI v4.0正式版暨《FineUI3to4一键升级工具》发布!
去年10-28号,我发布了一篇文章<海王星给你好看!FineUI v4.0公测版发布暨<你找BUG我送书>活动开始>,标志着FineUI开始向4.0版本迈进.经过4个月3个公测 ...
- HoloLens开发手记 - Unity之Tracking loss
当HoloLens设备不能识别到自己在世界中的位置时,应用就会发生tracking loss.默认情况下,Unity会暂停Update更新循环并显示一张闪屏图片给用户.当设备重新能追踪到位置时,闪屏图 ...
- CoordinatorLayout自定义Bahavior特效及其源码分析
@[CoordinatorLayout, Bahavior] CoordinatorLayout是android support design包中可以算是最重要的一个东西,运用它可以做出一些不错的特效 ...
- 由一次程序崩溃引起的对new表达式的再次学习
1. 起因 某天,一个同事跟我反馈说在windows上调试公司产品的一个交易核心时出现了使用未初始化的指针导致后台服务崩溃的情况.示例代码如下所示: struct sample { ][]; //.. ...
- 中继器、集线器(HUB)、网桥、交换机、路由器比较
中继器或集线器既不能隔离冲突域又不能隔离广播域,网桥或交换机只能隔离冲突域不能隔离广播域,路由器既能隔离冲突域又能隔离广播域,为什么?[解析] 首先要清楚什么是冲突域和广播域,当一块网卡发送信息时有可 ...
- 数据源DBCP一二
其实DBCP这个数据源实际上和com.alibaba.druid.pool.DruidDataSource 是差不多的
- 使用delegate实现简单的查询功能
protected void imgbtnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e) { string keyWo ...
- openvpn的部署
http://www.gaohuirong.cn/openvpn/2016/04/12/linux-install-openvpn.html
- Django- 1- 数据库设置
更改配置文件中的 字段更改为 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', //按照自己的数据库配置配置,现在所配置 ...