简体   繁体   English

C ++类型转换运算符

[英]C++ type conversion operator

I am studying operator overloading, there are some parts that are difficult to understand. 我正在研究运算符重载,有些部分很难理解。

See this example code. 请参阅此示例代码。

class A {
    private:
    char a;
    int b;
    double c;

public:
A(char _a = 'a', int _b = 99, double _c = 1.618) :a(_a), b(_b), c(_c){
}

public:
    operator char() const {
        cout << "operator char() called" << endl;
        return this->a;
    }

operator int() const {
        cout << "operator int() called" << endl;
        return this->b;
    }

operator double() {
        cout << "operator double() called" << endl;
        return this->c;
    }
};
int main(void) {
    A a;
    char b = a;
    int c = a;
    double d = a;

    printf("%c\n", b);
    printf("%d\n", c);
    printf("%f\n", d);

    return 0;
}

I made this code to test for type conversion operator and expected that the appropriate function would be called for each type of data. 我制作了这个代码来测试类型转换运算符,并期望为每种类型的数据调用适当的函数。

But the result is.. 但结果是......

operator double() called
operator double() called
operator double() called
    <-- strange character is gone on board!
1
1.618000

I can not understand why the results are not as follows. 我不明白为什么结果不如下。

operator char() called
operator int() called
operator double() called
a
99
1.618

Why is double operator called when converting to char and int? 为什么在转换为char和int时调用double运算符?

Have a good day! 祝你有美好的一天! :) :)

You forgot the const on the double conversion operator: 您忘记了double转换运算符上的const

operator double() const {  // <---------------------------
        cout << "operator double() called" << endl;
        return this->c;
    }
};

As in your example a is not const , the double conversion is the best match. 在您的示例中, a不是const ,双转换是最佳匹配。 If you fix that you get the expected output. 如果你修复了,你得到了预期的输出。

Live example 实例

...some opinion based PS: ......一些基于意见的PS:

I didnt find what the core guidelines say about conversion operators, but if I had to make up a guideline for conversion operators it would be: Avoid them. 我没有找到关于转换运算符的核心指导原则,但如果我必须为转换运算符编制指南,那就是:避免使用它们。 If you use them, make them explicit . 如果您使用它们,请explicit它们。 The surprising effects of implicit conversion outweigh the benefits by far. 隐式转换的惊人效果远远超过了它的好处。

Just as an example, consider std::bitset . 举个例子,考虑一下std::bitset Instead of offering conversion operators it has to_string , to_ulong and to_ullong . 它不是提供转换运算符,而是具有to_stringto_ulongto_ullong It is better to have your code explicit. 最好将代码显式化。 A a; double d = a; is a little bit mysterious. 有点神秘。 I would have to look at the class definition to get an idea of what is really going on. 我必须查看类定义以了解实际情况。 On the other hand A a; double d = a.as_double(); 另一方面A a; double d = a.as_double(); A a; double d = a.as_double(); can do the exact same thing, but is way more expressive. 可以做同样的事情,但更具表现力。

Yea so the problem is, that you made all operators const except for the double operator. 是的问题是,除了double运算符之外,你使所有运算符都为const。 I am still a bit surprised because this const just means that the operator call does not modify the class members. 我仍然有点惊讶,因为这个const只意味着操作符调用不会修改类成员。 Still it seems that only the double operator is called for all 3. I would all 3 op's make const and then it will work properly. 似乎只有双重运算符被调用所有3.我将所有3个运算符构成const然后它将正常工作。

If someone has an explanation why this happens, I would also like to know. 如果有人解释为什么会这样,我也想知道。 Cheers. 干杯。

operator char() const { // here is const
    cout << "operator char() called" << endl;
    return this->a;
}

operator int() const { // here is const
    cout << "operator int() called" << endl;
    return this->b;
}

operator double() { // here is no const
    cout << "operator double() called" << endl;
    return this->c;
}

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

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