由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 再一个问题c++
相关主题
请教C++11的rvalue refanother c++ interview question
C++ operator = overloading用copy & swap有啥优点请问const myClass &src 和myClass const &src有什么区别?
const int foo()啥意思?前几天有人问rvalue reference的
请问在class member function中如何调用overloaded function ca请教这个程序里用到了什么constructor啊?有几个copy constructor?
C++糟粕和需要避免的。C++:如何计算一个类实例化了多少次?
warning: returning address of local variable or temporaryC++ 什么时候用 "new" ?
stl Compare为何需要重载()?问一个C++ String的初始化问题
今天给c++震惊了C++怎样设置全局变量
相关话题的讨论汇总
话题: const话题: operator话题: rhs话题: int话题: myclass
进入Programming版参与讨论
1 (共1页)
H*M
发帖数: 1268
1
class X
{
public:
X& operator=(const X& rhs);
const X& operator+(const X& rhs) const;
const X& operator+(int m);
private:
int n;
};
int main()
{
X a,b,c;
a=a=b+c;
a=a+5+c;
a=b+c+5; //wrong here..why?
a=b+5;
(c=a+a)=b+c;
}
X****r
发帖数: 3557
2
前一句 a=a+5+c;就应该出错了。你的operator +返回的是const X&,
而operator +自己的*this不是const,所以operator +的结果不能再
调用operator +。你把operator +改为
const X& operator+(const X& rhs) const;
const X& operator+(int m) const;
就对了。

【在 H*M 的大作中提到】
: class X
: {
: public:
: X& operator=(const X& rhs);
: const X& operator+(const X& rhs) const;
: const X& operator+(int m);
: private:
: int n;
: };
: int main()

t****t
发帖数: 6806
3
operator+为什么会返回const X&?
不应该返回const X吗?

【在 H*M 的大作中提到】
: class X
: {
: public:
: X& operator=(const X& rhs);
: const X& operator+(const X& rhs) const;
: const X& operator+(int m);
: private:
: int n;
: };
: int main()

t****t
发帖数: 6806
4
这些写法(applying const wherever you can)是标准的做法, 没有特殊要求的话都应
该这样做. 反过来说, 如果不这样做, 必须要有合适的理由, 否则使用这class的人就
会受到一些怪怪的限制. 表面上这题是考C++的概念, 但是如果经常写程序, 这些所谓
的概念根本就不算是问题. 所以这其实就是看你是不是经常写程序.
我问你operator+为什么会返回const X&, 那是因为如果返回const X&, 程序里多半还
会有这样那样的问题. 返回新对象就多半没事.

【在 H*M 的大作中提到】
: class X
: {
: public:
: X& operator=(const X& rhs);
: const X& operator+(const X& rhs) const;
: const X& operator+(int m);
: private:
: int n;
: };
: int main()

l******u
发帖数: 5
5
这么多好小伙都在搞这些语法的繁琐的细节 唉 还是不学C++算了
p***o
发帖数: 1252
6
你学来干嘛

【在 l******u 的大作中提到】
: 这么多好小伙都在搞这些语法的繁琐的细节 唉 还是不学C++算了
k****5
发帖数: 546
7
operator +自己的*this不是const,所以operator +的结果不能再
调用operator +
这是什么原因? 我的理解是,从non-const 到 const 应该是允许的。
b********r
发帖数: 1080
8

我的理解是a=a+5+c先执行a+5, 返回的是const&, 也就是a本身.但这个const a
的operator + 在做+c 时企图改变*this的值,所以不对.
我觉得应该改成
X& operator+(const X& rhs);
X& operator+(const int m);
才对.就是去掉前后的const.

【在 X****r 的大作中提到】
: 前一句 a=a+5+c;就应该出错了。你的operator +返回的是const X&,
: 而operator +自己的*this不是const,所以operator +的结果不能再
: 调用operator +。你把operator +改为
: const X& operator+(const X& rhs) const;
: const X& operator+(int m) const;
: 就对了。

p***o
发帖数: 1252
9
你说a+5返回a本身,那你算的a+5到哪里去了...

【在 b********r 的大作中提到】
:
: 我的理解是a=a+5+c先执行a+5, 返回的是const&, 也就是a本身.但这个const a
: 的operator + 在做+c 时企图改变*this的值,所以不对.
: 我觉得应该改成
: X& operator+(const X& rhs);
: X& operator+(const int m);
: 才对.就是去掉前后的const.

H****r
发帖数: 2801
10
Why return &X instead of X?
It seems nature to operator +=, -= etc. but not here?

【在 H*M 的大作中提到】
: class X
: {
: public:
: X& operator=(const X& rhs);
: const X& operator+(const X& rhs) const;
: const X& operator+(int m);
: private:
: int n;
: };
: int main()

H****r
发帖数: 2801
11
In textbook or C++ tutorial you'll find examples like:
// Define operator += ... ...
// Add this instance's value to other, and return a new instance
// with the result.
const MyClass MyClass::operator+(const MyClass &other) const
{
MyClass result = *this; // Make a copy of myself.
result += other; // Use += to add other to the copy.
return result; // All done!
}

【在 H****r 的大作中提到】
: Why return &X instead of X?
: It seems nature to operator +=, -= etc. but not here?

b********r
发帖数: 1080
12
说到底就是为啥用reference. 如果不用的就没这些麻烦.

【在 p***o 的大作中提到】
: 你说a+5返回a本身,那你算的a+5到哪里去了...
1 (共1页)
进入Programming版参与讨论
相关主题
C++怎样设置全局变量C++糟粕和需要避免的。
一个Quant Developer的C++面试题warning: returning address of local variable or temporary
问个copy constructor的问题stl Compare为何需要重载()?
怎么隐藏c++template代码?只提供lib 不提供头文件今天给c++震惊了
请教C++11的rvalue refanother c++ interview question
C++ operator = overloading用copy & swap有啥优点请问const myClass &src 和myClass const &src有什么区别?
const int foo()啥意思?前几天有人问rvalue reference的
请问在class member function中如何调用overloaded function ca请教这个程序里用到了什么constructor啊?有几个copy constructor?
相关话题的讨论汇总
话题: const话题: operator话题: rhs话题: int话题: myclass