C++库研究笔记——赋值操作符operator=的正确重载方式(三个准则)
来源:互联网 发布:百无一用是书生知乎 编辑:程序博客网 时间:2024/05/16 12:14
C++ Operator Overloading Guidelines
设计要求:
a, b, c, d, e;
a = b = c = d = e = 42;
This is interpreted by the compiler as:
a = (b = (c = (d = (e = 42))));
MyClass a, b, c;
...
(a = b) = c; // What??
// Take a const-reference to the right-hand side of the assignment.
// Return a non-const reference to the left-hand side.
//对应上条
MyClass& MyClass::operator=(const MyClass &rhs) {
... // Do the assignment operation!
return *this; // Return a reference to myself.
}
但上述设计仍然不完整,a=a会出现什么情况? 当然还要考虑性能问题
MyClass mc;
...
mc = mc; // BLAMMO.
CHECK FOR SELF-ASSIGNMENT.
所以正确且安全的设计如下:So, the correct and safe version of the MyClass assignment operator would be this:
总之,重载操作符的三个准则
1.右端值要为const 类型(不想改变他的值)Take a const-reference for the argument (the right-hand side of the assignment).
2.要返回一个引用,以便于实现(a=b=c…… (Do this by returning *this.)
a<<(b<<c) 与上文一样的
当然如果我们只允许a<<b非连锁的操作(指a<<b<<c不允许),我们可以抛弃掉规则2
最终设计:
MyClass& MyClass::operator=(const MyClass &rhs) { // Check for self-assignment! if (this == &rhs) // Same object? return *this; // Yes, so skip assignment, and just return *this. ... // Deallocate, allocate new space, copy values... return *this; }
设计要求:
a, b, c, d, e;
a = b = c = d = e = 42;
This is interpreted by the compiler as:
a = (b = (c = (d = (e = 42))));
MyClass a, b, c;
...
(a = b) = c; // What??
// Take a const-reference to the right-hand side of the assignment.
// Return a non-const reference to the left-hand side.
//对应上条
MyClass& MyClass::operator=(const MyClass &rhs) {
... // Do the assignment operation!
return *this; // Return a reference to myself.
}
MyClass& MyClass::operator=(const MyClass &rhs) { // 1. Deallocate any memory that MyClass is using internally // 2. Allocate some memory to hold the contents of rhs // 3. Copy the values from rhs into this instance // 4. Return *this }
但上述设计仍然不完整,a=a会出现什么情况? 当然还要考虑性能问题
MyClass mc;
...
mc = mc; // BLAMMO.
CHECK FOR SELF-ASSIGNMENT.
所以正确且安全的设计如下:So, the correct and safe version of the MyClass assignment operator would be this:
MyClass& MyClass::operator=(const MyClass &rhs) { // Check for self-assignment! if (this == &rhs) // Same object? return *this; // Yes, so skip assignment, and just return *this. ... // Deallocate, allocate new space, copy values... return *this; }
或者:
MyClass& MyClass::operator=(const MyClass &rhs) { // Only do assignment if RHS is a different object from this. if (this != &rhs) { ... // Deallocate, allocate new space, copy values... } return *this; }
总之,重载操作符的三个准则
1.右端值要为const 类型(不想改变他的值)Take a const-reference for the argument (the right-hand side of the assignment).
2.要返回一个引用,以便于实现(a=b=c…… (Do this by returning *this.)
3.检查是否为自我赋值Check for self-assignment, by comparing the pointers (this to &rhs).
-----------------------------------------------------
a<<(b<<c) 与上文一样的
当然如果我们只允许a<<b非连锁的操作(指a<<b<<c不允许),我们可以抛弃掉规则2
void MyClass::operator=(const MyClass &rhs) { // Only do assignment if RHS is a different object from this. if (this != &rhs) { ... // Deallocate, allocate new space, copy values... } return *this; }
- C++库研究笔记——赋值操作符operator=的正确重载方式(三个准则)
- C++库研究笔记——赋值操作符operator=的正确重载方式(2)
- C++ 重载赋值操作符operator=的高级议题
- 重载赋值操作operator=()
- C++库研究笔记——操作符重载实现类型转换&这样做的意义
- 【C++】operator关键字(重载操作符)
- 正确的重载operator+
- 正确的重载operator
- 正确的重载operator+
- 正确的重载operator+
- c++重载操作符研究之 operator ->()
- 极客班 c++(下)第二周学习笔记 ——重载 operator new & operator delete
- C++ 操作符重载 operator = 、operator <
- C++ 操作符重载 operator = 、operator <
- [C/C++]_[操作符重载operator type()和operator()的区别]
- C++ operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- C++,重载输出操作符<<, cout.operator()
- iOS应用崩溃日志分析
- 设计模式
- Core-site.xml配置
- Android 4.0 Camera
- Cloud Foundry 源码解析一览(warden)
- C++库研究笔记——赋值操作符operator=的正确重载方式(三个准则)
- Android 关于onNewIntent的方法调用
- mysql /*! */的含义
- hdu 4620 多校第二场 搜索
- 如何强制客户端刷新缓存
- nginx配置文件
- python的断点调试
- 【91xcz】教你玩转windows7快捷键
- OpenRisc-41-or1200的cache模块分析