简体   繁体   English

是否为此重载运算符调用了转换构造函数? (C ++)

[英]Is a conversion constructor called for this overloaded operator? (C++)

Given that this is the only == function in the class, 鉴于这是该类中唯一的==函数,

bool myClass::operator == ( const myClass & rhs )
{
    if ( var1 == rhs.var1 )
        return true;
    else
        return false;
}

What does the following comparison assume? 以下比较假定什么?

myClass mc1;
anotherClass ac1;

if ( mc1 == ac1 )

My instinct was to say that it assumes that ac1 will be converted to type myClass, but how can that happen? 我的直觉是说它假设ac1将被转换为myClass类型,但是那怎么会发生呢? operator== has a parameter of type myClass & rhs, so how could myClass's conversion constructor be called when ac1 is passed to the function? operator ==具有类型为myClass&rhs的参数,那么当将ac1传递给函数时,如何调用myClass的转换构造函数?

Thanks! 谢谢!

The parameter is of type const myClass & so a conversion call is called, a temporary is constructed and a const-reference to such a temporary is passed to the function. 该参数的类型为const myClass &因此将调用转换调用,构造一个临时变量,并将对该临时变量的const引用传递给该函数。 Upon return, the temporary is destroyed. 归还后,临时设施将被销毁。

The compiler takes the left hand side and tries to find the right operator for it. 编译器采用左侧,并尝试为其找到合适的运算符。

So it will take the == operator, and try to fit the right hand side to a myClass by casting. 因此,它将使用==运算符,并尝试通过铸造使右侧适合myClass

If it can't find implicit casting to myClass it will return a compilation error. 如果找不到对myClass的隐式转换,则将返回编译错误。

The compiler given mc1 == ac1 will search for the best match for the operator==(A,B) using the normal overload resolution rules for any function. 给定mc1 == ac1的编译器将使用正常的任何函数的重载解析规则来搜索运算符==(A,B)的最佳匹配。

Assuming it finds your bool myClass::operator==(const myClass&) function the unambiguously best match (this may not be the case. for example it may find an operator declared bool operator==(const myClass&, const anotherClass&) which is superior instead), it will then bind and make the necessary parameter conversions as it would for any function call. 假设它找到您的bool myClass::operator==(const myClass&)函数无疑是最佳匹配(情况可能并非如此。例如,它可能会发现一个声明为bool operator==(const myClass&, const anotherClass&)相反,它将绑定并进行必要的参数转换,就像对任何函数调用一样。

To make the conversion from an lvale of type anotherClass to a const myClass& (assuming anotherClass does not inherit from myClass, in which case no conversion would be needed) it will then search for a single (unambiguously best) converting constructor or conversion operator to turn the parameter into a myClass temporary and then execute the operator== call with that. 要从类型为anotherClass的lvale转换为const myClass& (假设anotherClass不继承自myClass,在这种情况下无需进行转换),它将搜索单个(无疑是最佳的) 转换构造函数转换运算符来转换将该参数放入myClass临时对象,然后执行该操作符==调用。

It the parameter of a function is a non-const reference than it will not consider performing such temporary conversion. 如果函数的参数是非常量引用,则它将不考虑执行此类临时转换。 The reason is that a non-const reference usually indicates that the function will perform some sideeffect on that parameter, which would be discarded when the temporary is destroyed leaving the original object uneffected - therefore it would most likely be a logic error accidently discarding this sideeffect than intentionally doing so - so the language designers disallowed it. 原因是非常量引用通常表明该函数将对该参数执行某些副作用,当临时变量被销毁而原始对象不受影响时,该副作用将被丢弃-因此,很可能是逻辑错误,意外地放弃了该副作用。而不是故意这样做-因此语言设计人员不允许这样做。

Since there's no perfect overload for that operator the converting constructor is called (it counts as an implicit conversion), and the temporary created in this way is passed to your operator== . 由于该操作符没有完美的重载,因此将调用转换构造函数(将其视为隐式转换),并将以此方式创建的临时传递给您的operator== After the call the temporary is destroyed. 通话后,临时站点被销毁。

Notice that this wouldn't happen if operator== accepted a non- const reference, because temporaries can be bound only to const references. 请注意,如果operator==接受非const引用,则不会发生这种情况,因为临时变量只能绑定到const引用。

Your assumption is correct. 您的假设是正确的。 The compiler will put it the conversion constructor call first, then call your == method with the converted object as the rhs argument. 编译器将首先调用转换构造函数,然后使用转换后的对象作为rhs参数调用==方法。

There are two other possible answers to your riddle, neither involving conversation constructors. 您的谜语还有两个可能的答案,都不涉及对话构造函数。

  1. There exists a free function, bool operator==(const myClass&, const &anotherClass); 有一个自由函数, bool operator==(const myClass&, const &anotherClass); , or , 要么

  2. anotherClass is publicly derived from myClass . anotherClass是从myClass公开派生的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM