C++ EXC_BAD_INSTRUCTION?

C++ EXC_BAD_INSTRUCTION?

写的class有什么问题,就是returns下面的错误:

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

当调用 operator= 时。

class test
{
    int x;
public:
    test()
    {
        this->x=10;
    }

    test(const test& other)
    {
        this->x=other.x;
    }

    test& operator=(const test& other)
    {
        this->x=other.x;
    }

};

您不会从 operator= 返回。这会调用未定义的行为。

test& operator=(const test& other)
{
   this->x=other.x;
}

您需要做的:

test& operator=(const test& other)
{
   this->x=other.x;
   return *this;
}