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 ...
随机推荐
- glibc memcpy() 源码浅谈
其实我本来只是想搞懂为什么memcpy()函数的参数类型是void *的: 我以为会在memcpy()源码中能找到答案,其实并没有,void *只是在传递参数的时候起了作用,可以让memcpy()接受 ...
- 跟着老猫来搞GO,基础进阶
回顾一下上一篇博客,主要是和大家分享了GO语言的基础语法,其中包含变量定义,基本类型,条件语句,循环语句.那本篇呢就开始和大家同步一下GO语言基础的进阶. 函数的定义 上次其实在很多的DEMO中已经写 ...
- 【python】以souhu邮箱为例学习DDT数据驱动测试
前言 DDT(Data-Driven Tests)是针对 unittest 单元测试框架设计的扩展库.允许使用不同的测试数据来运行一个测试用例,并将其展示为多个测试用例.通俗理解为相同的测试脚本使用不 ...
- 集合之Map接口
Map接口概述 Map与Collection并列存在.用于存储具有映射关系的数据 : key-value Map 中的 key 和 value 都可以是任何引用类型的数据 Map 中的 key 用Se ...
- 解决虚拟机安装linux系统无法全屏问题 & vmtools安装
修改设置 1) 如下图右单击虚拟机名,选择[settings-],调出虚拟机设置界面. 2) 在设置界面选择[hardware]->[CD/DVD2(IDE)]->[Connection] ...
- Java之JNDI注入
Java之JNDI注入 About JNDI 0x01 简介 JNDI(Java Naming and Directory Interface)是SUN公司提供的一种标准的Java命名系统接口,JND ...
- 终论到底该不该写isDebugEnabled
以前: 很多日志框架都不支持{}模板的写法(如Log4j1.X, Apache Commons Logging),于是只能通过字符串拼接来输出日志内容: log.debug("debug日志 ...
- ubuntu install redis
ubuntu install redis apt-get update apt-get install redis-server redis-server --daemonize yes
- Integer.valueOf()和Integer.parseInt()区别
他们返回类型的不同是最大的原因. static int parseInt(String s) 将字符串参数作为有符号的十进制整数进行分析. static Integer valueOf(int i) ...
- scrapy获取当当网中数据
yield 1. 带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代 2. yield 是一个类似 return 的关键字,迭代一次遇到yield时就返回yiel ...