C++ 重载转换运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10281334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:51:37  来源:igfitidea点击:

C++ Overloading Conversion Operators

c++operator-overloadingconst-correctness

提问by Mihai Todor

I am trying to have a class that allows implicit casting to certain built in types, like unsigned long int and since I'm trying to do this as correct as possible (this is my first important project in C++), I have hit a strange issue regarding const correctness:

我试图拥有一个允许隐式转换为某些内置类型的类,例如 unsigned long int 并且由于我试图尽可能正确地执行此操作(这是我在 C++ 中的第一个重要项目),我遇到了一个奇怪的问题关于 const 正确性的问题:

This works:

这有效:

#include <iostream>

class CustomizedInt
{
private:
    int data;
public:
    CustomizedInt();
    CustomizedInt(int input);
    operator unsigned long int () const
    {
        unsigned long int output;
        output = (unsigned long int)data;
        return output;
    }
};

CustomizedInt::CustomizedInt()
{
    this->data = 0;
}

CustomizedInt::CustomizedInt(int input)
{
    this->data = input;
}

int main()
{
    CustomizedInt x;
    unsigned long int y = x;

    std::cout << y << std::endl;

    return 0;
}

But this:

但是这个:

#include <iostream>

class CustomizedInt
{
private:
    int data;
public:
    CustomizedInt();
    CustomizedInt(int input);
    operator unsigned long int () const;
};

CustomizedInt::CustomizedInt()
{
    this->data = 0;
}

CustomizedInt::CustomizedInt(int input)
{
    this->data = input;
}

CustomizedInt::operator unsigned long()
{
    unsigned long int output;
    output = (unsigned long int)data;
    return output;
}

int main()
{
    CustomizedInt x;
    unsigned long int y = x;

    std::cout << y << std::endl;

    return 0;
}

gives me this error in Visual Studio 2010: error C2511: 'CustomizedInt::operator unsigned long(void)' : overloaded member function not found in 'CustomizedInt'

在 Visual Studio 2010 中给我这个错误: error C2511: 'CustomizedInt::operator unsigned long(void)' : overloaded member function not found in 'CustomizedInt'

Now, if I remove the keyword const from the operator definition, everything is OK. Is this a bug? I read that I'm supposed to use the const keyword after each (public) method / operator in order to clearly state that it does not alter the current object in any way.

现在,如果我从运算符定义中删除关键字 const,则一切正常。这是一个错误吗?我读到我应该在每个(公共)方法/运算符之后使用 const 关键字,以便清楚地说明它不会以任何方式改变当前对象。

Also, I know that defining such an operator may be poor practice, but I am not sure I fully understand the associated caveats. Could somebody please outline them? Would it be better practice to just define a public method called ToUnsignedLongInt?

另外,我知道定义这样的运算符可能是不好的做法,但我不确定我是否完全理解相关的警告。有人可以概述它们吗?定义一个名为 ToUnsignedLongInt 的公共方法会更好吗?

回答by Praetorian

The function signature does not match the function definition.

函数签名与函数定义不匹配。

operator unsigned long int () const;

and

CustomizedInt::operator unsigned long()    { ... }
                                       ^^^
                                   const missing

In this case you should mark the conversion operator as constsince it doesn't affect the internal state of the object.

在这种情况下,您应该将转换运算符标记为const因为它不会影响对象的内部状态。

Also, use constructor initialization lists to initialize your member variables.

此外,使用构造函数初始化列表来初始化您的成员变量。

CustomizedInt::CustomizedInt()
: data()
{
}

CustomizedInt::CustomizedInt(int input)
: data(input)
{
}

回答by Jerry Coffin

You couldremove the constfrom the declaration, but what you almost certainly want to do is addit to the definition:

可以const从声明中删除,但您几乎肯定想要做的是添加到定义中:

CustomizedInt::operator unsigned long() const
{
    unsigned long int output;
    output = (unsigned long int)data;
    return output;
}

回答by Oliver Charlesworth

Yes, if your member function doesn't affect the logical state of the object, then you should indeed postfix it with const, so that the compiler will enforce that.

是的,如果您的成员函数不影响对象的逻辑状态,那么您确实应该使用 后缀const,以便编译器强制执行。

But in that case, you also need to add constwhen you define the function body!

但是那样的话,你还需要const在定义函数体的时候加上!

回答by acraig5075

You just need to copy the same function prototype into the implementation. ie.

您只需要将相同的函数原型复制到实现中。IE。

CustomizedInt::operator unsigned long int() const