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;
}