简体   繁体   English

C ++中的隐式转换

[英]Implicit conversion in C++

In C++, I'm trying to use implicit conversion with a conditional operator. 在C ++中,我试图使用条件运算符进行隐式转换。 Consider this example: 考虑这个例子:

class MyFloat
{ 
    public:                                                                    
        MyFloat(float val){m_val = val;}
        operator float(){return m_val;}
    protected:
        float m_val;
};

int main(int argc, char **argv)
{
    MyFloat a = 0.5f;
    MyFloat b = 1.0f;                            
    float x = true ? a-0.5f : b;
    return 0;
}

It causes a compiler error: 它会导致编译器错误:

error: operands to ?: have different types ‘MyFloat’ and ‘float’

I expect the conditional operator to implicitly convert b to the type of a-0.5 , float. 我希望条件运算符隐式地将b转换为a-0.5 ,float的类型。 But this does not happen. 但这不会发生。 How do I achieve this implicit cast? 我如何实现这种隐式演员?

Ideally, I want to avoid a static cast or an accessor method like float MyFloat::getValue() . 理想情况下,我想避免静态float MyFloat::getValue()或访问器方法,如float MyFloat::getValue()

The problem is that there are two conversions. 问题是有两次转换。 The compiler can convert a-0.5 to MyFloat or it can convert b to float . 编译器可以将a-0.5转换为MyFloat ,也可以将b转换为float As long as you have both conversions and neither is marked explicit you'll get this kind of ambiguity all the time. 只要您同时进行了两次转换,并且两者都没有explicit标记,您就会始终获得这种模糊性。

Only some conversions are done for you. 只为您完成了一些转换。 From http://msdn.microsoft.com/en-us/library/e4213hs1(v=vs.71).aspx 来自http://msdn.microsoft.com/en-us/library/e4213hs1(v=vs.71).aspx

The first operand must be of integral or pointer type. 第一个操作数必须是整数或指针类型。 The following rules apply to the second and third expressions: 以下规则适用于第二个和第三个表达式:

  • If both expressions are of the same type, the result is of that type. 如果两个表达式的类型相同,则结果为该类型。
  • If both expressions are of arithmetic or enumeration types, the usual arithmetic - conversions (covered in Arithmetic Conversions) are performed to convert them to a common type. 如果两个表达式都是算术类型或枚举类型,则执行通常的算术转换(在算术转换中涵盖)以将它们转换为通用类型。
  • If both expressions are of pointer types or if one is a pointer type and the other is a constant expression that evaluates to 0, pointer conversions are performed to convert them to a common type. 如果两个表达式都是指针类型或者如果一个是指针类型而另一个是一个计算结果为0的常量表达式,则执行指针转换以将它们转换为公共类型。
  • If both expressions are of reference types, reference conversions are performed to convert them to a common type. 如果两个表达式都是引用类型,则执行引用转换以将它们转换为公共类型。
  • If both expressions are of type void, the common type is type void. 如果两个表达式都是void类型,则常见类型为void类型。
  • If both expressions are of a given class type, the common type is that class type. 如果两个表达式都是给定的类类型,则通用类型是该类类型。

The ternary operator doesn't do any implicit casting if I remember correctly. 如果我没记错的话,三元运算符不会进行任何隐式转换。 You will need to either write 你需要写

static_cast<MyFloat>(a-0.5) 

or 要么

static_cast<float>(b). 

I'll dig through the C++ standards doc when I get home for more details. 当我回到家中获取更多细节时,我将深入研究C ++标准文档。

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

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