由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - const X& operator+(const X& rhs) const;
相关主题
[合集] thinking in c++ 这书如何?C 取地址和加法速度比较
请有C++数值计算的同学帮忙看看,问题不难魏老师偷偷地通过加core和网卡来升级自己的系统
what does this statement mean?an+b复杂度为什么是O(n^2), Θ(n)?
interview questionsc++ cast problem
C++ code explanationAbout volatile in C
[合集] c++的dynamic_cast是如何实现的?Array in C
[help] how do I improve my coding quality?fortran 77 introduction book recommendation?
C++ 普及课程 (视频):Understanding rvalue and lvaluehow to get this size?
相关话题的讨论汇总
话题: const话题: rhs话题: operator话题: mem话题: reference
进入Programming版参与讨论
1 (共1页)
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 的大作中提到】
: 通过你的解释,你一句话,我豁然开朗。这个设计我想明白了。多谢了。
: 但是我现在觉得返回常量引用更好。
: 不知道你同意不同意?

1 (共1页)
进入Programming版参与讨论
相关主题
how to get this size?C++ code explanation
what's the purpose of pointer to pointers?[合集] c++的dynamic_cast是如何实现的?
做题了,做题了,看谁能搞清楚[help] how do I improve my coding quality?
曝个湾区大公司前一段发生的事吧 (转载)C++ 普及课程 (视频):Understanding rvalue and lvalue
[合集] thinking in c++ 这书如何?C 取地址和加法速度比较
请有C++数值计算的同学帮忙看看,问题不难魏老师偷偷地通过加core和网卡来升级自己的系统
what does this statement mean?an+b复杂度为什么是O(n^2), Θ(n)?
interview questionsc++ cast problem
相关话题的讨论汇总
话题: const话题: rhs话题: operator话题: mem话题: reference