变量的归属问题?

chenhuach 2007-11-18 06:00:56
#include>iostream?
using namespace std;
class CRect{
int length,width;
public:
CRect(){};
CRect(int l,int w){
length=l;width=w;}
void disp(){
cout<<"The area is:"<<length*width<<endl;}
CRect operator,(CRect r){
CRect temp;temp.length=r.lengthltemp.width=r.width;
return temp;}
CRect operator+(CRect r){
CRect temp;temp.length=r.length+length;temp.width=r.width+width;//temp.length=r.length+length中的第三个length哪个的!(第一个是temp的,第二个是r的)
return temp;}
};
int main(){
CRect r1(3,3),r2(5,8),r3(2,4);
r1.disp();r2.disp();r3.disp();
r1=(r1,r2+r3,r4);r1.disp();
return 0;}
...全文
105 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyustc 2007-11-20
  • 打赏
  • 举报
回复
调用对象的length
0黄瓜0 2007-11-20
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

class CRect{
int length,
width;
public:
CRect(){};
CRect(int l,int w){
length=l;width=w;} //不要在代码后放后花括号.也不要在前花括号后抵倒写代码.
void disp(){
cout << "The area is: " <<length*width <<endl;} //不要在代码后放后花括号.
CRect operator,(CRect r){
CRect temp;temp.length=r.lengthltemp.width=r.width;
return temp;}
CRect operator+(CRect r){
CRect temp;
temp.length=r.length+length;//与temp.length=r.length+this->length;相同
temp.width=r.width+width;
return temp;
}
};
int main(){
CRect r1(3,3),r2(5,8),r3(2,4);
r1.disp();r2.disp();r3.disp();
r1=(r1,r2+r3,r4);r1.disp();
return 0;}//不要在代码后放后花括号.
legendwin 2007-11-20
  • 打赏
  • 举报
回复
r1+r2的意思是r1调用重载算术函数+
意思是r1是this指针指向的对象,而r2是传入算术运算符函数的变量。
great2328 2007-11-19
  • 打赏
  • 举报
回复
错了
比如代码里的r2+r3,第三个length是r2的
相当于是:r2.operator+(r3),参数中隐藏了this指针
great2328 2007-11-19
  • 打赏
  • 举报
回复
比如代码里的r2+r3,第三个length是r1的
相当于是:r2.operator+(r3),参数中隐藏了this指针
xiao_dang 2007-11-18
  • 打赏
  • 举报
回复
+左边对象的length
chenhuach 2007-11-18
  • 打赏
  • 举报
回复
我现在就在看运算符重载,可看不懂!
caixingxin 2007-11-18
  • 打赏
  • 举报
回复
建议看一下运算符重载就知道那个length是什么了~~
caixingxin 2007-11-18
  • 打赏
  • 举报
回复
汗..看错..不好意思~~
caixingxin 2007-11-18
  • 打赏
  • 举报
回复
int length,width;//是这个lenth

64,649

社区成员

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

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