为什么在这种情况下友元重载运算符优于转换运算符

发布于 2024-08-25 09:29:34 字数 587 浏览 7 评论 0原文

您好,我有这样的代码,我认为朋友重载运算符和转换运算符都有类似的功能。但是,为什么在这种情况下会调用友元重载运算符呢?规则是什么?

非常感谢!

class A{

    double i;
public:
    A(int i):i(i) {}
    operator double () const { cout<<"conversion operator"<<endl;return i;}                            // a conversion operator
    friend bool operator>(int i, A  a);                            // a friend funcion of operator >
};

bool operator>(int i, A  a ){
    cout<<"Friend"<<endl;
    return i>a.i;
}
int main()
{
    A  aa(1);
     if (0 > aa){
         return 1;
      }
}

Hi I have a code like this, I think both the friend overloaded operator and conversion operator have the similar function. However, why does the friend overloaded operator is called in this case? What's the rules?

Thanks so much!

class A{

    double i;
public:
    A(int i):i(i) {}
    operator double () const { cout<<"conversion operator"<<endl;return i;}                            // a conversion operator
    friend bool operator>(int i, A  a);                            // a friend funcion of operator >
};

bool operator>(int i, A  a ){
    cout<<"Friend"<<endl;
    return i>a.i;
}
int main()
{
    A  aa(1);
     if (0 > aa){
         return 1;
      }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

墨落画卷 2024-09-01 09:29:34

调用重载的operator> 不需要转换。为了调用内置的 operator>,需要进行一次转换(用户定义的转换运算符。重载解析更喜欢所需转换次数较少的选项,因此重载的 operator>< 。

请注意,例如,如果您要将重载的 operator> 的定义更改为:

friend bool operator>(double i, A  a);

您将收到编译错误,因为重载的 operator> 都已重载 code> 和内置 operator> 将需要一次转换,并且编译器将无法解决歧义。

No conversion is necessary for the overloaded operator> to be called. In order for the built-in operator> to be called, one conversion is necessary (the user-defined conversion operator. Overload resolution prefers options with fewer required conversions, so the overloaded operator> is used.

Note that if you were to change the definition of your overloaded operator> to be, for example:

friend bool operator>(double i, A  a);

you would get a compilation error because both the overloaded operator> and the built-in operator> would require one conversion, and the compiler would not be able to resolve the ambiguity.

苦行僧 2024-09-01 09:29:34

我并不是说我的答案得到了标准的支持,但让我们逻辑地思考一下。

当你点击这条线时:

0 > aa

你有两个选择。您可以致电提供的运营商:

friend bool operator>(int i, A  a);

100% 兼容,或者您​​可以进行两次转换以到达目的地!你会选择哪一个?

I am not claiming that my answer is supported by the standards, but lets think about it logically.

When you hit this line:

0 > aa

You have two options. Either you call the provided operator:

friend bool operator>(int i, A  a);

Which is 100% compatible, or you can do two conversions to reach your destination! Which one would you choose?

大姐,你呐 2024-09-01 09:29:34

如果添加转换运算符,则 A 类型的对象可能会在您最意想不到的时候转换为 double。

一个好的程序不会提供一种方法让他的类被意外使用,并且转换运算符为该类提供了在许多意想不到的情况下使用的机会(通常情况下,您期望编译时错误的情况并不是因为自动类型转换)。

因此,应该谨慎对待转换运算符(和单参数构造函数),因为编译器可能会在您最不期望的时候进行转换。

If you add a conversion operator then an object of type A may be converted to double when you least expect it.

A good program does not provide a way for his classes to be accidently used and the conversion operator opens up the opertunity for the class to be used in a whole host of unintended situations (normally situations where you would expect a compile time error are not because of the auto type conversion).

As a result conversion operators (and single argument constructors) should be treateed with some care because of situations were the compiler may do conversion when you least expect it.

桃扇骨 2024-09-01 09:29:34

之所以调用它,是因为它在表达式 0 > 的上下文中是完全匹配。 aa。事实上,很难弄清楚你是如何提出“为什么”这个问题的。按照逻辑,如果在这种情况下朋友没有被打电话,人们会想到“为什么”问题。

如果将表达式更改为 0.0 > aa,该调用将变得不明确,因为任何一条路径都会比另一条路径更好。

It is called because it is an exact match in the context of the expression 0 > aa. In fact, it is hard to figure out how you came up with the "why" question. By logic, one'd expect a "why" question if the friend weren't called in this case.

If you change the expression to 0.0 > aa, the call will beciome ambiguous, because neuther path will be better than the other.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文