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:

  1. First, implicit type conversion to T is applied if necessary.
  2. Then, the resulting value is placed in a temporary variable of type T.
  3. 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的更多相关文章

  1. References & the Copy-Constructor

    1 There are certain rules when using references: (Page 451) A reference must be initialized when it ...

  2. const引用返回值

    一.引用 引用是别名 必须在定义引用时进行初始化.初始化是指明引用指向哪个对象的唯一方法. const 引用是指向 const 对象的引用: ; const int &refVal = iva ...

  3. 非const引用不能指向临时变量

    没找到具体原因,MSDN看到下面这句,VC是从2008才有这一限制的,感觉就是从语法上对临时变量增加了限定,因为一般说来修改一个临时变量是毫无意义的,通过增加限定,强调临时变量只读语义.虽然实际上修改 ...

  4. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  5. 总结c++ primer中的notes

    转载:http://blog.csdn.net/ace_fei/article/details/7386517 说明: C++ Primer, Fourth Edition (中英文)下载地址:htt ...

  6. 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 ...

  7. 阅读Google的C++代码规范有感

    李开复曾在微博上说过,Google的C++代码规范是全球最好的一份C++代码规范,没有之一.最近花了点时间看了下这份代码规范,收获确实很大,在编程过程中一些乱七八糟的坏习惯也该改一改了.最新的英文版见 ...

  8. (转) Functions

    Functions Functions allow to structure programs in segments of code to perform individual tasks. In ...

  9. 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 ...

随机推荐

  1. 记 FineUI 官方论坛所遭受的一次真实网络攻击!做一个像 ice 有道德的黑客!

    在开始正文之前,请帮忙为当前 排名前 10 唯一的 .Net 开源软件 FineUI  投一票: 投票地址: https://code.csdn.net/2013OSSurvey/gitop/code ...

  2. Word 打包 zip 并提供下载

    该篇博客记录Java Web项目将word打包zip并提供下载功能的实现和其中遇到的坑,方便后续自己的查看的参照. 1. 后台处理的java 方法 首先将所有的word生成到uploadword目录下 ...

  3. 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)

    传统解法 题目来自 leetcode 335. Self Crossing. 题意非常简单,有一个点,一开始位于 (0, 0) 位置,然后有规律地往上,左,下,右方向移动一定的距离,判断是否会相交(s ...

  4. 提高Visual Studio开发性能的几款插件

    通过打开Visual Studio,单机TOOLS—Extensions and Updates-Online-Visual Studio Gallery(工具-扩展和更新-联网-Visual Stu ...

  5. 通过Ajax实现增删改查

    项目链接:https://github.com/shuai7boy/Ajax_CRUD 简要截图:

  6. 东大OJ-1430-PrimeNumbers

    题目描述 I'll give you a number , please tell me how many different prime factors in this number. 输入 The ...

  7. 1019mysql 复制技术

    -- 第一步实现主从复制参照 http://369369.blog.51cto.com/319630/790921/核心点 :开启二进制日子和服务器ID,创建复制账号,配置连接主从服务器,查看各自状态 ...

  8. MySQL server PID file could not be found!

    重启mysql提示MySQL server PID file could not be found! Starting MySQL...The server quit without updating ...

  9. 超级详细的iptable教程文档

    Iptabels是与Linux内核集成的包过滤防火墙系统,几乎所有的linux发行版本都会包含Iptables的功能.如果 Linux 系统连接到因特网或 LAN.服务器或连接 LAN 和因特网的代理 ...

  10. 关于selenium 3.0 + python 3.5中多层框架或窗口的定位driver.switch_to_frame()

    针对selenium3 中的窗口定位会自动划掉,不起作用 现在换成 driver.switch_to.frame()就会不报错了