64,631
社区成员
发帖
与我相关
我的任务
分享
one one::operator +(one k)
{
return one(x + y);//这里,左侧对象的x和y的和来初始化构造函数one(int i=10, int j=20的形参i, 而形参j取缺省值20,所以operator+返回的对象就不是你想要的了;
}
#include <iostream>
using namespace std;
class one
{
private:
int x;
int y;
public:
one(int i=10, int j=20):x(i),y(j){}
one operator+(const one &k);
void dispaly();
};
one one::operator +(const one &k)
{
return one(x + k.x, y + k.y);//这里, 这样改是你要的结果吗?
}
void one::dispaly()
{
cout<<"x + y = "<<x+y<<endl;
}
int main()
{
one c1(1, 2), c2(3, 3), c3;
c3 = c1 + c2 ;
c1.dispaly();
c2.dispaly();
c3.dispaly();
system("pause");
return 0;
}
one one::operator +(one k)
{
return one(x+y); // x+y等价于this->x +this->y 和K没关系
}