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. 安装RedHat和Centos后做的15件事情

    由于之前的Centos 7不支持无线网络连接,我尝试着将内核升级至4.8还是无效,遂决定换回RedHat 7,目前系统已经安装好,版本是Red Hat Enterprise Linux 7.3,下面是 ...

  2. iNeuOS工业互联网操作系统,发布实时存储方式:实时存储、变化存储、定时存储,增加设备振动状态和电能状态监测驱动,v3.6.2

    目       录 1.      概述... 1 2.      平台演示... 2 3.      存储方式... 2 4.      设备状态和用电状态监控驱动... 3 1.   概述 本次升 ...

  3. docker容器命令(一)

    容器命令 创建容器:docker run 参数: -it 交互 -d 后台 –name 容器名 -p 主机端口:容器端口 (主机端口映射到docker端口) docker run --name cen ...

  4. Java发展的重大事故

    1990年,在Sun计算机公司中,由Patrick Naughton.Mi keSheridan 及 James Gosling领导的小组Green Team,开发出的新的程序语言,命名为0ak, 后 ...

  5. 第三周PTA笔记 回文数+A-B(大数减法)+高精度除法+数楼梯(大数加法)

    回文数 对于一个自然数n,若将n的各位数字反向排列所得的数n1与n相等,则称n为回文数,例如2332. 若给定一个N( 2<=N<=16)进制数M(M的长度在一百位以内),如果M不是回文数 ...

  6. vue.js3 学习笔记 (一)——mixin 混入

    vue 2 中采用选项式API.如:data.methods.watch.computed以及生命周期钩子函数等等. mixin 混入,提供了一种非常灵活的方式,来分发 vue 组件中的可复用功能,一 ...

  7. 博主日常工作中使用的shell脚本分享

    前言: 今天给大家分享一篇在我工作中常用的一个shell脚本,里面有一些我们常用到的shell操作.该脚本用于本地电脑和服务器交互上,实现以下功能: 自动拉取自己个人电脑上的源码到服务器上yocto包 ...

  8. hover 背后的数学和图形学

    前端开发中,hover是最常见的鼠标操作行为之一,用起来也很方便,CSS直接提供:hover伪类,js可以通过mouseover+mouseout事件模拟,甚至一些第三方库/框架直接提供了 hover ...

  9. [noi239]count

    将每一个ai表示为$ai=ki\cdot m+ri$,即满足$m\sum ki+\sum ri=n$且$0<ri<m$枚举$S=\sum ri$(S范围是$k\le S\le k(m-1) ...

  10. 数字逻辑实践2->Verilog编写规范

    来源:数字逻辑与Verilog设计实验课讲解,个人做的笔记与整理. 00 规范的重要性 良好的编程风格有利于减少消耗的硬件资源,提高设计的工作频率 . 提高系统的可移植性和可维护性. 程序的格式化能体 ...