【发布时间】:2011-02-01 16:35:43
【问题描述】:
我已经获得了具有 int 变量 x 和 y 的私有类,以及一个运算符重载函数,
class Bag{
private:
int x;
int y;
public:
Bag();
~Bag();
//.......
//.....etc
};
Bag operator+ (Bag new) const{
Bag result(*this); //what does this mean?
result.x += new.x;
result.y += new.y;
}
有“Bag result(*this);”的效果是什么那里?。
【问题讨论】:
-
operator+函数是否缺少return语句? -
这看起来不像是有效的 C++ -- new is 关键字
-
如果你想创建运算符,我建议查看
Boost.Operators。他们将类似的运算符组合在一起(例如+=和+),并且只编写其中一个组可以免费授予您其他组:)
标签: c++ class operator-overloading this