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的更多相关文章

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

  2. Effective C++ 第0章 copy constructor和copy assignment operator

    拷贝构造函数(copy constructor)被用来以一个对象来初始化同类型的另一个对象,拷贝赋值运算符(copy assignment operator)被用来将一个对象中的值拷贝到同类型的另一个 ...

  3. copy constructor和copy assignment operator的区别

    拷贝构造函数(copy constructor)被用来以一个对象来初始化同类型的另一个对象,拷贝赋值运算符(copy assignment operator)被用来将一个对象中的值拷贝到同类型的另一个 ...

  4. Copy constructor vs assignment operator in C++

    Difficulty Level: Rookie Consider the following C++ program. 1 #include<iostream> 2 #include&l ...

  5. Lintcode208 Assignment Operator Overloading (C++ Only) solution 题解

    [题目描述] Implement an assignment operator overloading method. Make sure that: The new data can be copi ...

  6. Could not find default endpoint element that references contract 'wcfXXXXXXXXXXX' in the ServiceMode

    Service本身没有问题,但是调用的时候,只在DataAccessSilverlight里引用了,而在主工程WebGISDemo里没有引用服务PowerDataServiceReference,所以 ...

  7. How to use base class's assignment operator in C++

    看了android下的代码,好长时间没看了,有个关于C++的知识点不是很清楚,查了下: 如何使用基类中的赋值运算符? 引述自http://stackoverflow.com/questions/122 ...

  8. PythonStudy——赋值运算符 Assignment operator

    eg: num = 10 num += 1 # 等价于 num = num + 1 => 11 print(num) 特殊操作: 1.链式赋值 a = b = num print(a, b, n ...

  9. 标准模板库(STL)的一个 bug

    今天敲代码的时候遇到 STL 的一个 bug,与 C++ 的类中的 const 成员变量有关.什么,明明提供了默认的构造函数和复制构造函数,竟然还要类提供赋值运算符重载.怎么会这样? 测试代码 Tes ...

随机推荐

  1. SpringCloud升级之路2020.0.x版-30. FeignClient 实现重试

    本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 需要重试的场景 微服务系统中,会遇到在线发布,一般的发布更新策略是:启动一个新的,启动成功 ...

  2. 【.NET 与树莓派】用 MPD 制作数字音乐播放器

    树莓派的日常家居玩法多多,制作一台属于自己的数字音乐播放机是其中的一种.严格上说,树莓派是没有声卡的,其板载的 3.5 mm 音频孔实际是通过 PWM 来实现音频输出的(通过算法让PWM信号变成模拟信 ...

  3. mapper接口绑定异常

    前言 由于MP的代码生成器把mapper接口绑定的写sql语句xml文件创建在java目录下,而Maven加载机制只会将.java文件编译成.class文件,所以在target目录下找不到写xml文件 ...

  4. css 跑马灯加载特效

    css 跑马灯加载特效 <!DOCTYPE html> <html lang="en"> <head> <meta charset=

  5. 在Winform中直接录入表格数据和在Vue&Elment中直接录入表格数据的比较

    一般来说,录入数据的时候,我们都采用在一个窗体界面中,根据不同内容进行录入,但是有时候涉及主从表的数据录入,从表的数据有时候为了录入方便,也会通过表格控件直接录入.在Winform开发的时候,我们很多 ...

  6. [hdu7020]Array

    (这是一个线性的做法) 显然对于合法的区间,众数是唯一的,因此不妨枚举众数,将众数标记为1.其余数标记为-1,此时问题即求有多少个区间和大于0 考虑暴力的做法:从左到右枚举右端点,记当前前缀和为$to ...

  7. [cf1434E]A Convex Game

    暴力求SG,结论:每一个序列的SG上限为$\sqrt{2\max a_{i}}+1$ 证明:将SG的转移看作一张DAG,归纳每一个点的SG值不超过其开始的最长路,显然成立 那么本题中最长路即在$a_{ ...

  8. nginx得请求转发代码-将请求转发到网关

    首先:本地主机host更改成 192.168.111.1 gulimail.com 这样一访问网址就能映射到本地. 然后修改nginx得conf worker_processes 1; events ...

  9. 解决FastJson中"$ref重复引用"的问题方法

    对象的引用重复使用造成了重复引用问题,Fastjson默认开启引用检测将相同的对象写成引用的形式: 1 2 3 4 5 {"$ref": "$"} // 引用根 ...

  10. DPC++中的现代C++语言特性

    Ⅰ DPC++简介 DPC++是Data Parallel C++(数据并行C++)的首字母缩写,它是Intel为了将SYCL引入LLVM和oneAPI所开发的开源项目.SYCL是为了提高各种加速设备 ...