m*********a 发帖数: 3299 | 1 简单的返回一个right hand value?
所以不能支持(a=b)=c:
MyClass MyClass::operator=(const MyClass &other){
if (this!=&other)
value=other.value;
return *this;
}
返回一个reference,可以用 left hand value, 支持(a=b)=c:
MyClass & MyClass::operator=(const MyClass &other){
if (this!=&other)
value=other.value;
return *this;
} |
D***n 发帖数: 6804 | 2 除了引发各种混淆没有任何优点,这些都是C++的糟粕。
我最讨厌的就是C++经常在很多基本程序下面塞自动代码。这些东西的本意是帮助开发
人员,但是实际上经常被滥用。
而且一旦有BUG简直是灾难。特别是大一点的项目,要穿上百层calling frames去debug。
【在 m*********a 的大作中提到】 : 简单的返回一个right hand value? : 所以不能支持(a=b)=c: : MyClass MyClass::operator=(const MyClass &other){ : if (this!=&other) : value=other.value; : return *this; : } : 返回一个reference,可以用 left hand value, 支持(a=b)=c: : MyClass & MyClass::operator=(const MyClass &other){ : if (this!=&other)
|
p***o 发帖数: 1252 | 3 你这些代码和copy&swap毫无关系。
【在 m*********a 的大作中提到】 : 简单的返回一个right hand value? : 所以不能支持(a=b)=c: : MyClass MyClass::operator=(const MyClass &other){ : if (this!=&other) : value=other.value; : return *this; : } : 返回一个reference,可以用 left hand value, 支持(a=b)=c: : MyClass & MyClass::operator=(const MyClass &other){ : if (this!=&other)
|
m*********a 发帖数: 3299 | 4 我是没有看到用copy&swap的必要.早我这个简单复制没有必要
【在 p***o 的大作中提到】 : 你这些代码和copy&swap毫无关系。
|
w***g 发帖数: 5958 | 5 我写了这么多年C++了,没碰到需要用到重载=,或者需要定义copy constructor啥的这
种情况。如果=不是默认语义,干脆就认为是non-copyable。否则都是自找麻烦。
【在 m*********a 的大作中提到】 : 简单的返回一个right hand value? : 所以不能支持(a=b)=c: : MyClass MyClass::operator=(const MyClass &other){ : if (this!=&other) : value=other.value; : return *this; : } : 返回一个reference,可以用 left hand value, 支持(a=b)=c: : MyClass & MyClass::operator=(const MyClass &other){ : if (this!=&other)
|
d********u 发帖数: 5383 | 6 operator overloading为什么要用copy constructor,我目测这里98%的人都不懂。
当然,他们可以说他们跟A3大妈是一个级别的。印度千人大系,澳洲tester,偶也!
【在 m*********a 的大作中提到】 : 简单的返回一个right hand value? : 所以不能支持(a=b)=c: : MyClass MyClass::operator=(const MyClass &other){ : if (this!=&other) : value=other.value; : return *this; : } : 返回一个reference,可以用 left hand value, 支持(a=b)=c: : MyClass & MyClass::operator=(const MyClass &other){ : if (this!=&other)
|
a*********a 发帖数: 3656 | 7 exactly my thought.
【在 p***o 的大作中提到】 : 你这些代码和copy&swap毫无关系。
|
p***o 发帖数: 1252 | 8 按成员复制/赋值编译器就能做,自己写属于画蛇添足。
【在 m*********a 的大作中提到】 : 我是没有看到用copy&swap的必要.早我这个简单复制没有必要
|
m*********a 发帖数: 3299 | 9 *this=other;
return *this;
就行了.
【在 p***o 的大作中提到】 : 按成员复制/赋值编译器就能做,自己写属于画蛇添足。
|
p***o 发帖数: 1252 | 10 你在operator=里写*this=other?
【在 m*********a 的大作中提到】 : *this=other; : return *this; : 就行了.
|
|
|
D***n 发帖数: 6804 | 11 关键这个规则不总是成立。比如刚刚讨论过的std::thread,那个的A=B的时候就不是
copy而是move,而且如果A原来是一个正在运行的线程,A=B会导致原来的线程停止....
【在 d********u 的大作中提到】 : operator overloading为什么要用copy constructor,我目测这里98%的人都不懂。 : 当然,他们可以说他们跟A3大妈是一个级别的。印度千人大系,澳洲tester,偶也!
|
h**********c 发帖数: 4120 | 12 c 在unix 时代很重要的一个,很头痛的一个就是
防止对kernel space 的随意访问,所以有了 const
有了 const带来了要变化就要copy |
m*********a 发帖数: 3299 | 13 在MyClass & operator=(const MyClass & other)中
swap(other);//copy other
return *this;
在MyClass 做swap other
class MyClass{
void swap(MyClass &other){
std::swap(this->value,other.value);
}
}
【在 p***o 的大作中提到】 : 你在operator=里写*this=other?
|
N******K 发帖数: 10202 | 14 std::move 有了 你还用swap这种远古社会的东西?
【在 m*********a 的大作中提到】 : 在MyClass & operator=(const MyClass & other)中 : swap(other);//copy other : return *this; : 在MyClass 做swap other : class MyClass{ : void swap(MyClass &other){ : std::swap(this->value,other.value); : } : }
|
p***o 发帖数: 1252 | 15 那段code根本就是错的。
【在 N******K 的大作中提到】 : std::move 有了 你还用swap这种远古社会的东西?
|
m*********a 发帖数: 3299 | 16 move 是move other内存的内容,=是copy other 内存的内容
如果你move, 然后delete other, 那么this就指向other的已经释放的内存了
这不是=要的,我觉得
如果other中的variable都是atomic类型的话,shallow copy就行了,*this=other
如果other中有refence类型,就用std::swap进行deep copy
【在 N******K 的大作中提到】 : std::move 有了 你还用swap这种远古社会的东西?
|
m****c 发帖数: 1181 | 17 笑死人了,你个从加拿大转进美国的2逼,会个constructor就不知道自己是谁了
【在 d********u 的大作中提到】 : operator overloading为什么要用copy constructor,我目测这里98%的人都不懂。 : 当然,他们可以说他们跟A3大妈是一个级别的。印度千人大系,澳洲tester,偶也!
|