【问题标题】:C++ EXC_BAD_INSTRUCTION?C++ EXC_BAD_INSTRUCTION?
【发布时间】:2020-06-28 17:45:56
【问题描述】:

写的类有什么问题,返回如下错误:

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

};

【问题讨论】:

标签: c++ class operator-overloading


【解决方案1】:

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

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

你需要做的:

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

【讨论】:

    猜你喜欢
    • 2016-01-09
    • 2019-01-15
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    相关资源
    最近更新 更多