简体   繁体   English

operator= 用于不匹配的类型

[英]operator= for mismatched types

I have the code below where I am trying to create an operator= for my class and a char.我有下面的代码,我试图为我的 class 和一个字符创建一个 operator=。 The class has a constructor for the char. class 有一个用于 char 的构造函数。

It when I call the = operator it will go into the class and assign the class b correctly, but when it returns the byte1 isn't updated.当我调用 = 运算符时,它会将 go 放入 class 并正确分配 class b,但是当它返回时 byte1 未更新。 I'm not sure why that would be.我不确定为什么会这样。

Part of my class:我的 class 的一部分:

Byte operator=(unsigned char lhs)
{
    Byte b(lhs);
    return b;
}

int main()
{
    unsigned char c1{ 'a' };
    Binary::Byte byte1;
    byte1 = c1;
}

As @HTNW said I was failing to return *this.正如@HTNW 所说,我没有返回 *this。 Should be:应该:

Byte operator=(unsigned char lhs)

    {
        Byte b(lhs);
        *this = b;
        return *this;
    }

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

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