请教: 操作符+重载 和 const member function

yangbo76 2008-03-06 06:51:22
请高手帮忙解释一下,rhs类型是const X&, b+c返回怎么成 const X了?


class X{
public:
X& operator= (const X& rhs){ return *this;};
const X& operator+ (const X& rhs) const{ return rhs;} ; //rhs类型是const X&, b+c返回怎么成 const X了?
const X& operator+ (int m){ return *this;};
private :
int n;
};

int main()
{
X a,b,c;

//以下哪行编译有问题?
(c=a+a) = b+ c ;
//a=b+c+5; //编译为何出错? Error: The operation "const X + int" is illegal.
a=a+5+c;
a=b+5;
a=a=b+c;

}

...全文
108 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangbo76 2008-03-06
  • 打赏
  • 举报
回复

试了一下,明白了。
[Quote=引用 6 楼 Chappell 的回复:]
简单的说:const对象只能调用类中的const成员函数。
[/Quote]

Thanks.
[看 <effective C++> 2e item 23]
taodm 2008-03-06
  • 打赏
  • 举报
回复
看<effective C++>2e item 23
yangbo76 2008-03-06
  • 打赏
  • 举报
回复
我不是故意的,网上的考题,准备面世用的

[Quote=引用 6 楼 Chappell 的回复:]
简单的说:const对象只能调用类中的const成员函数。
所以修改const X& operator+ (int m)const{ return *this;};
而且提醒一下,+运算符一般都不是返回this.所以貌似应该是改成const X operator+(int m) const
……除非你是故意的,呵呵
[/Quote]
Chappell 2008-03-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yangbo76 的回复:]
以上已经定义了operator+ (int m),
而且a=a+5+c;
a=b+5;
都是可以的。
只是 a=b+c+5;编译通不过。
[/Quote]

a=b+5;
可以的原因是b不是const对象,所以可以调用你程序里面的operator+(int m)
yangbo76 2008-03-06
  • 打赏
  • 举报
回复
谢谢3楼。
这样说的话,只要参数传入(b+c)是const的,成员函数调用就需要用const吗?


[Quote=引用 3 楼 xalangying 的回复:]
const X& operator+ (const X& rhs) const 返回的结果是临时变量,因为不应该存在给临时变量赋值的情况,所以使之为const,如(b+c)=a; 这样的表达式是不合理的,写成返加的是const 后这表达式就编译报错了

//a=b+c+5; //编译为何出错? Error: The operation "const X + int" is illegal.
错在 +5 , b+c 的结果是const的(如上面所说),而你的const X& operator+ (int m)并不是const成员函数,所以报错了, 应该把const …
[/Quote]
Chappell 2008-03-06
  • 打赏
  • 举报
回复
简单的说:const对象只能调用类中的const成员函数。
所以修改const X& operator+ (int m)const{ return *this;};
而且提醒一下,+运算符一般都不是返回this.所以貌似应该是改成const X operator+(int m) const
……除非你是故意的,呵呵
jixingzhong 2008-03-06
  • 打赏
  • 举报
回复
const X& operator+ (int m) const { return *this;};
yangbo76 2008-03-06
  • 打赏
  • 举报
回复
以上已经定义了operator+ (int m),
而且a=a+5+c;
a=b+5;
都是可以的。
只是 a=b+c+5;编译通不过。
xalangying 2008-03-06
  • 打赏
  • 举报
回复
const X& operator+ (const X& rhs) const 返回的结果是临时变量,因为不应该存在给临时变量赋值的情况,所以使之为const,如(b+c)=a; 这样的表达式是不合理的,写成返加的是const 后这表达式就编译报错了

//a=b+c+5; //编译为何出错? Error: The operation "const X + int" is illegal.
错在 +5 , b+c 的结果是const的(如上面所说),而你的const X& operator+ (int m)并不是const成员函数,所以报错了, 应该把const X& operator+ (int m){ return *this;}; 改为
const X& operator+ (int m)const{ return *this;};
jaymin 2008-03-06
  • 打赏
  • 举报
回复
错了,
是:
int main()
{
X a,b,c,d;
d=X(5);
(c=a+a)=b+c;
a=b+c+d;
a=a+d+c;
a=b+d;
a+a+b+c;
return 0;
}
jaymin 2008-03-06
  • 打赏
  • 举报
回复
编译出错是由于5不是X类型;
可以这样:
class X{
public:
X(int n);//构造函数
X& operator= (const X& rhs){ return *this;};
X& operator+ (const X& rhs) const{ return rhs;} ;
X& operator+ (int m){ return *this;};
~X();//析构函数
private :
int n;
};

int main()
{
X a,b,c,d;
d=X(5);
(c=a+a) = b+ c ;
//a=b+c+5; //
a=a+5+c;
a=b+5;
a=a=b+c;
}

64,849

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧