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到哪里去了...
|