【发布时间】:2015-10-17 10:51:31
【问题描述】:
假设我有一个班级编号
class Number
{
public:
int numb;
Number (int g)
{
numb = g;
}
int operator+(int h)
{
return this->numb+h;
}
};
当我尝试使用我的重载运算符时
cout << 3 + s; // doesn't work
cout << s + 3;
我明白为什么它不起作用,但我不知道如何使它适用于3 + s
当然,我可以在类外编写带有 2 个参数的 operator+,但我想让我的运算符在类中重载。
我用谷歌搜索了它,但没有找到任何解决方案。
【问题讨论】: