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 ...
随机推荐
- Webwork 学习之路【06】Action 调用
一路走来,终于要开始 webwork 核心业务类的总结,webwork 通过对客户端传递的 web 参数重新包装,进行执行业务 Action 类,并反馈执行结果,本篇源码分析对应下图 WebWork ...
- win7下IIS配置以及域名映射方法
win7下IIS配置以及域名映射方法 第一步:打开控制面板,选择程序与功能,如下图: 第二步:双击打开程序与功能面板,如下图: 第三步:打开”打开或关闭windows功能”(红线圈起来的地方),如下图 ...
- 彻底理解Toast原理和解决小米MIUI系统上没法弹Toast的问题
1.Toast的基本使用 Toast在Android中属于系统消息通知,用来提示用户完成了什么操作.或者给用户一个必要的提醒.Toast的官方定义是这样的: A toast provides simp ...
- Windows10一周年庆典壁纸
example: 下载:http://pan.baidu.com/s/1b55D5k
- Tomcat遇到的问题
1. java.lang.OutOfMemoryError: PermGen space 启动tomcat服务时,报这个错,查了下是,内存泄露 PermGen space的全称是Permanent G ...
- PHP Apache 配置伪静态
1.首先是开启rewrite_module(如何开启,百度搜索) 2.创建.htaccess文件(如何创建,百度搜索) 3.在.htaccess文件中打开重写服务:RewriteEngine On 4 ...
- extjs5 一个容器中有几个组件公用一个控制器和一个模型
Ext.define('TestViewModel', { extend: 'Ext.app.ViewModel', alias: 'viewmodel.test', // connects to v ...
- jsp内置对象作业3-application用户注册
1,注册页面 zhuCe.jsp <%@ page language="java" contentType="text/html; charset=UTF-8&qu ...
- C#元组示例详解
元组的概要: 数组合并了相同类型的对象,而元组合并了不同类型的对象.元组起源于函数编程语言(如F#) ,在这些语言中频繁使用元组.在N盯4中,元组可通过.NET Fmmework用于所有的NET语言. ...
- js-处理回车事件
/**回车 */ function enterkey() { //兼容IE或其它其它浏览器 var event = arguments[0] || window.event; //兼容IE或其它浏览器 ...