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 ...
随机推荐
- DOM 元素节点几何量与滚动几何量
当在 Web 浏览器中查看 HTML 文档时,DOM 节点被解析,并被渲染成盒模型(如下图),有时我们需要知道一些信息,比如盒模型的大小,盒模型在浏览器中的位置等等,本文我们就来详细了解下元素节点的几 ...
- 高性能JavaScript DOM编程
我们知道,DOM是用于操作XML和HTML文档的应用程序接口,用脚本进行DOM操作的代价很昂贵.有个贴切的比喻,把DOM和JavaScript(这里指ECMScript)各自想象为一个岛屿,它们之间用 ...
- MATLAB调用C程序、调试和LDPC译码
MATLAB是一个很好用的工具.利用MATLAB脚本进行科学计算也特别方便快捷.但是代码存在较多循环时,MATLAB运行速度极慢.如果不想放弃MATLAB中大量方便使用的库,又希望代码能迅速快捷的运行 ...
- C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别
C#中HashTable.Dictionary.ConcurrentDictionar三者都表示键/值对的集合,但是到底有什么区别,下面详细介绍 一.HashTable HashTable表示键/值对 ...
- ul、li模仿ios的TableView实现城市选择
最近项目一个接着一个,之前说的精创环的项目还没做完,今天说先把那个放一下,先做访客系统,销售会见客户之后可以对客户进行一个跟踪记录,原型图也给了,今日头条的频道自定义页面一样. 如果是在IOS上让我来 ...
- C#版的MapReduce
如果不知道MapReduce是怎么工作的,请看这里,如果不知道MapReduce是什么,请google之! 今天“闲”来无事,忽想起C#里没有MapReduce的方法,构思之,coding之: #re ...
- jquery+bootstrap使用数字增减按钮
<div class="container"> <div class="page-header"><h1>Bootstrap ...
- 微信公众平台消息接口开发之微信浏览器HTTP_USER_AGENT判断
在微信公众平台的开发过程中,我们有时需要开发网页并判断是否是是来自微信浏览器访问,本文介绍如何做出这一判断. 一.$_SERVER数组 $_SERVER 是一个包含了诸如头信息(header).路径( ...
- springMVC之applicationcontext.xml配置说明
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- find常见用法
Linux中find常见用法示例 ·find path -option [ -print ] [ -exec -ok command ] {} \; find命令的参数 ...