C ++重载转换运算符 [英] C++ Overloading Conversion Operators

查看:99
本文介绍了C ++重载转换运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个类允许隐式转换到某些内置类型,如unsigned long int,因为我试图做这正确的可能(这是我的第一个重要的项目在C + +),我有碰到一个关于const正确性的奇怪问题:

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:

这个工作原理:

#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;
}

但是:

#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;
}

在Visual Studio 2010中给我这个错误:错误C2511:'CustomizedInt :: operator unsigned long(void)':'CustomizedInt'中找不到重载成员函数

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

关键字const来自运算符定义,一切正常。这是一个错误?我读到,我应该在每个(公共)方法/操作符后使用const关键字,以清楚地说明它不以任何方式改变当前对象。

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.

另外,我知道定义这样的运算符可能是不好的做法,但我不确定我是否完全理解相关的警告。有人请大概说一下吗?最好只是定义一个名为ToUnsignedLongInt的公共方法。

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?

推荐答案

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

The function signature does not match the function definition.

operator unsigned long int () const;

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

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

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

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

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

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

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

这篇关于C ++重载转换运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆