重载运算符中没有隐式转换
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;
}
d1 + 4
works but 4 + d1
doesn't even though 4 can be converted implicitly to a GMan. Why aren't they equivalent?
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调用
x + y
由 C++ 编译器翻译为以下两个调用之一(取决于x
是否为类类型,以及是否存在这样的函数):会员功能
自由函数
现在,C++ 有一个简单的规则:在成员访问运算符 (
.
) 之前不能发生隐式转换。这样,上面代码中的 x 就无法在第一个代码中进行隐式转换,但在第二个代码中可以。这个规则是有道理的:如果
x
可以在上面的第一个代码中隐式转换,C++ 编译器将不再知道要调用哪个函数(即它属于哪个类),因此它必须在所有现有类中搜索匹配的成员函数。这会对 C++ 的类型系统造成严重破坏,并使重载规则变得更加复杂和混乱。A call
x + y
is translated by the C++ compiler into either of the following two calls (depending on whetherx
is of class type, and whether such a function exists):Member function
Free function
Now C++ has a simple rule: no implicit conversion can happen before a member access operator (
.
). That way,x
in the above code cannot undergo an implicit conversion in the first code, but it can in the second.This rule makes sense: if
x
could be converted implicitly in the first code above, the C++ compiler wouldn’t know any more which function to call (i.e. which class it belongs to) so it would have to search all existing classes for a matching member function. That would play havoc with C++’ type system and make the overloading rules even more complex and confusing.这个答案是正确的。这些要点需要实现此类运算符的规范方法:
对于其他运算符,依此类推。
This answer is correct. Those points then entail the canonical way of implementing such operators:
And so on for other operators.