Default Assignment Operator and References
We have discussed assignment operator overloading for dynamically allocated resources here . This is a an extension of the previous post. In the previous post, we discussed that when we don’t write our own assignment operator, compiler created assignment operator does shallow copy and that cause problems.
What happens when we have references in our class and there is no user defined assignment operator.
For example, predict the output of following program.
1 #include<iostream>
2 using namespace std;
3
4 class Test
5 {
6 int x;
7 int &ref;
8 public:
9 Test (int i):x(i), ref(x)
10 {
11 }
12 void print()
13 {
14 cout << ref;
15 }
16 void setX(int i)
17 {
18 x = i;
19 }
20 };
21
22 int main()
23 {
24 Test t1(10);
25 Test t2(20);
26 t2 = t1;
27 t1.setX(40);
28 t2.print();
29 return 0;
30 }
Output:
Compiler Error: non-static reference member 'int& Test::ref', can't use default assignment operator
Compiler doesn’t creates default assignment operator in following cases:
1. Class has a nonstatic data member of a const type or a reference type
2. Class has a nonstatic data member of a type which has an inaccessible copy assignment operator
3. Class is derived from a base class with an inaccessible copy assignment operator
When any of the above conditions is true, user must define assignment operator.
For example, if we add an assignment operator to the above code, the code works fine without any error.
1 #include<iostream>
2 using namespace std;
3
4 class Test
5 {
6 int x;
7 int &ref;
8 public:
9 Test (int i):x(i), ref(x)
10 {
11 }
12 void print()
13 {
14 cout << ref;
15 }
16 void setX(int i)
17 {
18 x = i;
19 }
20 Test &operator = (const Test &t)
21 {
22 x = t.x;
23 return *this;
24 }
25 };
26
27 int main()
28 {
29 Test t1(10);
30 Test t2(20);
31 t2 = t1;
32 t1.setX(40);
33 t2.print();
34 return 0;
35 }
Output: 10
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-26 22:01:46
Default Assignment Operator and References的更多相关文章
- When should we write our own assignment operator in C++?
The answer is same as Copy Constructor. If a class doesn't contain pointers, then there is no need t ...
- Effective C++ 第0章 copy constructor和copy assignment operator
拷贝构造函数(copy constructor)被用来以一个对象来初始化同类型的另一个对象,拷贝赋值运算符(copy assignment operator)被用来将一个对象中的值拷贝到同类型的另一个 ...
- copy constructor和copy assignment operator的区别
拷贝构造函数(copy constructor)被用来以一个对象来初始化同类型的另一个对象,拷贝赋值运算符(copy assignment operator)被用来将一个对象中的值拷贝到同类型的另一个 ...
- Copy constructor vs assignment operator in C++
Difficulty Level: Rookie Consider the following C++ program. 1 #include<iostream> 2 #include&l ...
- Lintcode208 Assignment Operator Overloading (C++ Only) solution 题解
[题目描述] Implement an assignment operator overloading method. Make sure that: The new data can be copi ...
- Could not find default endpoint element that references contract 'wcfXXXXXXXXXXX' in the ServiceMode
Service本身没有问题,但是调用的时候,只在DataAccessSilverlight里引用了,而在主工程WebGISDemo里没有引用服务PowerDataServiceReference,所以 ...
- How to use base class's assignment operator in C++
看了android下的代码,好长时间没看了,有个关于C++的知识点不是很清楚,查了下: 如何使用基类中的赋值运算符? 引述自http://stackoverflow.com/questions/122 ...
- PythonStudy——赋值运算符 Assignment operator
eg: num = 10 num += 1 # 等价于 num = num + 1 => 11 print(num) 特殊操作: 1.链式赋值 a = b = num print(a, b, n ...
- 标准模板库(STL)的一个 bug
今天敲代码的时候遇到 STL 的一个 bug,与 C++ 的类中的 const 成员变量有关.什么,明明提供了默认的构造函数和复制构造函数,竟然还要类提供赋值运算符重载.怎么会这样? 测试代码 Tes ...
随机推荐
- httprunner3源码解读(2)models.py
源码目录结构 我们首先来看下models.py的代码结构 我们可以看到这个模块中定义了12个属性和22个模型类,我们依次来看 属性源码分析 import os from enum import Enu ...
- 开发属于自己的包,并上传到npm上
1.DIY开发包 1.1符合标准的包结构 一个规范的包,它的组成结构,必须符合以下3点要求: 包必须以单独的目录而存在 包的顶级目录下,必须包含package.json这个包管理文件 package. ...
- ELK集群之metricbeat(9)
Metricbeat包的安装及简单使用 Metricbeat包的安装及简单使用 系统数据采集 Python -> ES -> Grafana metricbeat的安装 metricbea ...
- webpack 之 js语法检查eslint
webpack 之 js语法检查eslint // 用来拼接绝对路径的方法 const {resolve} = require('path') const HtmlWebpackPlugin = re ...
- postman调试工具介绍及常用的快捷键收集
关于Postman postman基础功能介绍 使用postman进行接口自动化测试 快捷键大全 简单操作 请求 工具栏 接口 窗口 数据编辑 关于Postman Postman是一款功能强大的网页调 ...
- jenkins bat删除指定路径下的文件及文件夹
最近在用jenkins集成,生成allure测试报告,但是每次生成的allure测试报告,都是上一次执行的痕迹.比如这次我只运行了100个用例,结果显示运行2000条,上一次运行的用例,时间也涵括了上 ...
- 通过修改host加速访问GitHub
加速访问GitHub 原理:绕过 DNS 解析,直接使用本地的 DNS 记录进行直接跳转. 可以通过 http://ping.chinaz.com/ 链接查询github的DNS信息,例如,可以直接打 ...
- [atARC114F]Permutation Division
由于是排列,即任意两个数字都各不相同,因此字典序最大的$q_{i}$就是将每一段的第一个数从大到小排序 接下来,考虑第一个元素,也就是每一段开头的最大值,分类讨论: 1.当$p_{1}\le k$时, ...
- 宝藏好物gRPCurl
宝物简介 grpcur是一个与grpc服务器交互的命令行工具,可认为是gRPC的curl工具. grpcurl用于从命令行调用gRPC服务器支持的RPC方法,gRPC使用二进制编码(protobuf) ...
- nrf52810/52832开发板能跑,自己的PCB不能跑的原因
1.PCB图片对比 这是我的开发板原理图: 这是我画的PCB原理图: 发现其实开发板上就是比我的多了两个外部晶振. 例程代码一般都是用外部晶振作为时钟,所以用例程的代码跑不通我的PCB. 2.解决办法 ...