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. 使用jax加速Hamming Distance的计算

    技术背景 一般认为Jax是谷歌为了取代TensorFlow而推出的一款全新的端到端可微的框架,但是Jax同时也集成了绝大部分的numpy函数,这就使得我们可以更加简便的从numpy的计算习惯中切换到G ...

  2. MySQL到底能否解决幻读问题

    先说结论,MySQL 存储引擎 InnoDB 在可重复读(RR)隔离级别下是解决了幻读问题的. 方法:是通过next-key lock在当前读事务开启时,1.给涉及到的行加写锁(行锁)防止写操作:2. ...

  3. 组件通信之全局事件总线 & 消息订阅发布

    全局事件总线 介绍 一种组件间通信的方式,适用于任意组件间通信. 在使用全局事件总线之前需要一些知识准备 所有组件实例的原型对象的原型对象就是 Vue 的原型对象,即VueComponent.prot ...

  4. 【Azure 环境】用 PowerShell 调用 AAD Token, 以及调用Azure REST API(如资源组列表)

    问题描述 PowerShell 脚本调用Azure REST API, 但是所有的API都需要进行权限验证.要在请求的Header部分带上Authorization参数,并用来对List Resour ...

  5. C语言二分查找法

    参考了C语言中折半查找法(二分法)的实现 二分查找算法(C语言实现) 先附上代码 #include<stdio.h> int BinSearch(int arr[],int len,int ...

  6. [cf461D]Appleman and Complicated Task

    假设该矩形是aij,那么有a(i,j)=a(i-1,j-1)^a(i-1,j+1)^a(i-2,j),不断递归下去可以发现a(i,j)=a(1,y-x+1)^a(1,y-x+3)^--^a(1,x+y ...

  7. [loj3315]抽卡

    令$S$表示对于某一种抽卡顺序中某一段长度为$k$的段全部被抽到的时间(这里没有期望)所构成的集合,根据$min-max$容斥的公式,有$E(\min(S))=\sum_{T\subseteq S}( ...

  8. 小程序嵌套H5的方式和技巧(二)

    文章接上文,小程序嵌套H5的方式和技巧(一) 四.刷新wev-view嵌套的H5页面 1)我们为什么要刷新wev-view嵌套的H5页面? 很多的业务场景都需要开发者每次打开页面都更新一下页面的数据. ...

  9. Error occurred during initialization of VM Could not reserve enough space fo

    通过es的elasticsearch.bat 启动.发现错误:Error occurred during initialization of VM Could not reserve enough s ...

  10. Apache Kafka分布式流处理平台及大厂面试宝典v3.0.0

    概述 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 Apache Kafka官网地址 http://kafka.apache.org/ 最新版本为 3.0.0 Apach ...