c++ why doesn't c++ allow rebinding a reference ?
1. The primary reason that the designer of C++ introduced reference into the language is to support "encapsulated" address passing (to or from a function).
C++ Primer 4th Edition Section 2.5:
In real-world programs, references are primarily used as formal parameters to functions.
If you're planning to return a reference from a function, you must be sure it's "safe" to do so. See C++ Primer 4th Edition Section 7.3.2 Subsection "Returning a Reference"
2. Another reason is the syntax. In Java, assignning to a reference always means "rebind this reference so it points to another object" (which is exactly the same meaning as you assign to a pointer in C++), note that in Java, primitive types don't have references, however in C++, primitive types can also have references, and assigning to a reference of a primitive type means "assign the value of the object on the right side of the = operator to the object pointed by the reference on the left side of the = operator", this meaning also applies on class-type objects. When you're aware of this fact, it's easy to see that if you want to "rebind" a reference to another object, you'll have to invent a new operator (if you still use the operator "=", how would the compiler know whether you're assigning value or rebinding a reference?) to stand for the "rebind" operation, which is unnecessary because you can just use pointers instead.
In short.
1. Reference is just designed to support glorified "address passing", primarily used as parameters and/or return values. Not to replace pointers.
2. Because pointers can "rebind", so we don't need the same thing twice.
c++ why doesn't c++ allow rebinding a reference ?的更多相关文章
- DNS重绑定DNS Rebinding攻击
DNS重绑定DNS Rebinding攻击 在网页浏览过程中,用户在地址栏中输入包含域名的网址.浏览器通过DNS服务器将域名解析为IP地址,然后向对应的IP地址请求资源,最后展现给用户.而对于域名所有 ...
- 【SEED Labs】DNS Rebinding Attack Lab
Lab Overview 实验环境下载:https://seedsecuritylabs.org/Labs_16.04/Networking/DNS_Rebinding/ 在这个实验中模拟的物联网设备 ...
- DNS Rebinding漏洞原理
目录 SSRF过滤器设计 背景知识 DNS TTL 公网DNS服务器 DNS重绑定 自建DNS服务器 利用步骤图解 实战中的注意事项 防御 参考 DNS Rebinding 广泛用于绕过同源策略.SS ...
- cx_Oracle摘记
由于想使用python操作oracle所以查看了cx_Oracle的官方文档,同时也查看了twisted中cx_Oracle的使用.下面是摘自文档中一些我认为有用的内容 cx_Oracle is a ...
- word20161218
QoS, Quality of Service / 服务质量 QoS Admission Control Service / QoS 许可控制服务 Quality of Service, QoS / ...
- Default Parameter Values in Python
Python’s handling of default parameter values is one of a few things that tends to trip up most new ...
- iOS为真机调试增加scribble来定位野指针
尽管在ARC中,野指针出现的频率已经大大降低了,但是仍然会有野指针困扰着我们. 在模拟器调试中,我们可以开启scribble或者zombieObject来将已经释放的内存填充无意义的内容,能够将一些非 ...
- jquery-jsrender使用
JsRender是一款基于jQuery的JavaScript模版引擎 特点: · 简单直观 · 功能强大 · 可扩展的 · 快如闪电 jsrender使用比较简单,本文简单结束一些常用的 使用过程 ...
- 【python】为什么修改全局的dict变量不用global关键字
转自:http://my.oschina.net/leejun2005/blog/145911?fromerr=qnPCgI19#OSC_h4_8 为什么修改字典d的值不用global关键字先声明呢? ...
随机推荐
- 如何移除inline-block元素之间的空白
我们想要的是<li>元素可以紧贴在一起,但是很显然,结果“出乎意料”.那么有什么方法可以让结果符合我们的预期呢?所能想到的解决方法至少有以下四种,而每种方法也都有其优劣所在,至于要如何选择 ...
- 两道Java面试题!
body { font-family: 微软雅黑; font-size: 14px; line-height: 2; } html, body { color: inherit; background ...
- 解决Ubuntu 14下,PhpStorm 9.x 编辑器界面中文乱码的问题
在Ubuntu 14中,安装了 PhpStorm 9.02,发现 软件界面中文乱码,但是源码编辑处却显示正常,如下图所示: 很奇怪,猜想,应该是软件界面字体有问题,选了一个没有包含中文字体的字体.先前 ...
- sql-server-on-linux-how-i-think-they-did-it : Anthony Nocentino's Blog
http://www.sqlservercentral.com/blogs/anthony-nocentinos-blog/2016/11/21/sql-server-on-linux-how-i-t ...
- 【java】处理时间字段 在数据库查询的时候只想要年月日,不想要时分秒 ,java中设置时间类型为年月日,java中设置Date中的时分秒为00.00.000
JDK8 中最简单的处理方式: @Test public void dateTest(){ Date now = new Date(); System.out.println(now); // jav ...
- ubuntu登录黑屏“failed to start session”, gdm+kdm+lightdm
sudo apt-get install ubuntu-desktop sudo systemctl start gdm sudo service lightdm restart sudo syste ...
- docker集群——Mesos集群下的负载均衡marathon-lb
前面的章节介绍了Mesos+Zookeeper+Marathon的Docker管理平台,接下来介绍如何在该平台下构建负载均衡. 默认情况下,mesos marathon会把app发布到随机节点的随机端 ...
- C# 0-1背包问题
0-1背包问题 0-1背包问题基本思想: p[i,j]表示在前面i个物品总价值为j时的价值最大值.str[i, j]表示在前面i个物品总价值为j时的价值最大值时的物品重量串. i=0 或者j=0时: ...
- Tornado框架的初步使用
Tornado的搭建很简单,使用pip,或者下载源码均可. 我们先看一个最简单的程序: import tornado.ioloop import tornado.web class MainHan ...
- asp.net 中点击button弹出模式对话框,选择值后返回到页面中(window.showModalDialog实现)
<td>现从事专业</td><td> <asp:TextBox ID="tbMajor" runat="server ...