超载运算符中没有隐性转换
-
09-10-2019 - |
题
d1 + 4
有效,但是 4 + d1
即使4可以隐式转换为Gman。他们为什么不等效?
struct GMan
{
int a, b;
GMan() : a(), b() {}
GMan(int _a) : a(_a), b() {}
GMan(int _a, int _b) : a(_a), b(_b) {}
GMan operator +(const GMan& _b)
{
GMan d;
d.a = this->a + _b.a;
d.b = this->b + _b.b;
return d;
}
};
int main()
{
GMan d1(1, 2), d(2);
GMan d3;
d3 = d1 + 4;
d3 = 4 + d1;
}
解决方案
一个电话 x + y
由C ++编译器翻译成以下两个电话中的任何一个(取决于是否是否 x
是类型,以及是否存在此类功能):
成员功能
x.operator +(y);
免费功能
operator +(x, y);
现在C ++有一个简单的规则:在成员访问操作员之前,不会发生任何隐式转换(.
)。那样, x
在上面的代码中,不能在第一个代码中进行隐式转换,但可以在第二个代码中进行。
此规则是有道理的:如果 x
可以在上面的第一个代码中隐式转换,C ++编译器将不再知道要调用哪个函数(即它属于哪个类),因此必须搜索 所有现有类 适用于匹配的成员函数。这将对C ++类型系统造成破坏,并使超载规则更加复杂和令人困惑。
其他提示
这个 答案是正确的。然后,这些要点需要实施此类操作员的规范方式:
struct GMan
{
int a, b;
/* Side-note: these could be combined:
GMan():a(),b(){}
GMan(int _a):a(_a),b(){}
GMan(int _a, int _b):a(_a),b(_b){}
*/
GMan(int _a = 0, int _b = 0) : a(_a), b(_b){} // into this
// first implement the mutating operator
GMan& operator+=(const GMan& _b)
{
// the use of 'this' to access members
// is generally seen as noise
a += _b.a;
b += _b.b;
return *this;
}
};
// then use it to implement the non-mutating operator, as a free-function
// (always prefer free-functions over member-functions, for various reasons)
GMan operator+(GMan _a, const GMan& _b)
{
_a += b; // code re-use
return _a;
}
依此类推。
不隶属于 StackOverflow