Default Assignment Operator and References
2013-11-26 22:02
246 查看
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.
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.
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
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.
#include<iostream> using namespace std; class Test { int x; int &ref; public: Test (int i):x(i), ref(x) { } void print() { cout << ref; } void setX(int i) { x = i; } }; int main() { Test t1(10); Test t2(20); t2 = t1; t1.setX(40); t2.print(); return 0; }
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.
#include<iostream> using namespace std; class Test { int x; int &ref; public: Test (int i):x(i), ref(x) { } void print() { cout << ref; } void setX(int i) { x = i; } Test &operator = (const Test &t) { x = t.x; return *this; } }; int main() { Test t1(10); Test t2(20); t2 = t1; t1.setX(40); t2.print(); return 0; }
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
相关文章推荐
- 【asp.net小札记】jquery+ajax绑定数据库显示table
- 点滴积累【JS】---JS小功能(offsetLeft实现图片滚动效果)
- SharePoint 2010自定义母版页小技巧——JavaScript和CSS引用
- jquery中prop()方法和attr()方法的区别
- javascript之事件学习(一)
- CSS3 pointer-events介绍
- 《了不起的Node.js》读后感
- JSF —— EL中可用隐式对象
- JavaScript中125个经典技巧
- Jquery点击数字切换图片
- Javascript 读书笔记(一)
- jquery div层级选择器
- jquery(全选、反选、添加、删除、移动元素)
- trim html
- css ul li 制作导航条
- hdu 1829 A Bug's Life 并查集(囧)
- ECMA-262,第 5 版 最新 JavaScript 规范
- javascript类型跟版本断定与检测_Javascript教程
- Jsp分页
- 15条JavaScript最佳实践【转】