z****e 发帖数: 2024 | 1 const X& operator+(const X& rhs) const;
看到一个声明如上,
const的mem fun 是不能改mem data吧?
那么这种设计干什么呢?进来的是const,里边的自己人也是const,加法,加谁呀? |
p***o 发帖数: 1252 | 2 The correct one should be:
X operator+(const X& rhs) const;
e.g. a=b+c is a=b.opeartor+(c), so neither b nor c will be changed.
【在 z****e 的大作中提到】 : const X& operator+(const X& rhs) const; : 看到一个声明如上, : const的mem fun 是不能改mem data吧? : 那么这种设计干什么呢?进来的是const,里边的自己人也是const,加法,加谁呀?
|
z****e 发帖数: 2024 | 3 返回值:
1.return by value
2.return by reference
3.return by const reference
我不知道理解的对不对,
2,比1高效对吧?
3,就是为了不改2对吧?
那么这个帖子里的程序,是3.而你给的方法是1.
1,比3,效率低对吧?
那为什么不用3?
【在 p***o 的大作中提到】 : The correct one should be: : X operator+(const X& rhs) const; : e.g. a=b+c is a=b.opeartor+(c), so neither b nor c will be changed.
|
z****e 发帖数: 2024 | 4 通过你的解释,你一句话,我豁然开朗。这个设计我想明白了。多谢了。
但是我现在觉得返回常量引用更好。
不知道你同意不同意?
【在 p***o 的大作中提到】 : The correct one should be: : X operator+(const X& rhs) const; : e.g. a=b+c is a=b.opeartor+(c), so neither b nor c will be changed.
|
t****t 发帖数: 6806 | 5 看上去你明白了, 实际上你没明白.
you should return a new object when you should.
See Effective C++, Item 21 (3rd Edition): Don't try to return a reference
when you must return an object.
I am sure Meyer explains much better than me.
【在 z****e 的大作中提到】 : 通过你的解释,你一句话,我豁然开朗。这个设计我想明白了。多谢了。 : 但是我现在觉得返回常量引用更好。 : 不知道你同意不同意?
|
z****e 发帖数: 2024 | 6 sure i must read it.
谨遵。
【在 t****t 的大作中提到】 : 看上去你明白了, 实际上你没明白. : you should return a new object when you should. : See Effective C++, Item 21 (3rd Edition): Don't try to return a reference : when you must return an object. : I am sure Meyer explains much better than me.
|
h*****0 发帖数: 4889 | 7 你返回个局部变量的引用,死翘翘了
【在 z****e 的大作中提到】 : 通过你的解释,你一句话,我豁然开朗。这个设计我想明白了。多谢了。 : 但是我现在觉得返回常量引用更好。 : 不知道你同意不同意?
|