我正在学习运算符重载。我正在尝试在代码中重载+运算符。当我使用隐式取消引用返回时,输出是乱码。

如果我在返回时显式取消引用该变量,则它工作正常。
发生问题的原因是因为我引用了一些临时变量,并且在超出范围后被销毁了。如果是这样,那么为什么显式取消引用有效?
附言:我知道我可以不加参考地返回,并且我没有遵循代码中的3规则。


class ComplexNum
{
private:
    int real, imaginary;
public:
    ComplexNum();
    ComplexNum(int x, int y);
    ComplexNum(const ComplexNum& other);
    ~ComplexNum();

    int getReal() const;
    int getImaginary() const;

    const ComplexNum& operator=(const ComplexNum&);
    friend std::ostream& operator <<(std::ostream& out, const ComplexNum& a);
    ComplexNum& operator+(const ComplexNum&);
};

ComplexNum::ComplexNum()
{
}

ComplexNum::ComplexNum(int x, int y):real(x), imaginary(y)
{
}

ComplexNum::ComplexNum(const ComplexNum& other)
{
    this->real = other.real;
    this->imaginary = other.imaginary;
}

ComplexNum::~ComplexNum()
{
}


int ComplexNum::getReal() const
{
    return real;
}

int ComplexNum::getImaginary() const
{
    return this->imaginary;
}

const ComplexNum& ComplexNum::operator=(const ComplexNum& other)
{
    real = other.real;
    imaginary = other.imaginary;

    return *this;
}

ComplexNum& ComplexNum::operator+(const ComplexNum& other)
{
    ComplexNum a(real + other.getReal(), imaginary + other.getImaginary());
    return a;
}

/*the above one doesn't work but the below commented out works fine.*/
/*
ComplexNum& ComplexNum::operator+(const ComplexNum& other)
{
    ComplexNum* a = new ComplexNum(real + other.getReal(), imaginary + other.getImaginary());
    return *a;
}*/

std::ostream& operator<<(std::ostream& out,const ComplexNum& a)
{
    out << a.real << " & " << a.imaginary << "j";
    return out;
}


/*Here is how I am calling it in main*/
int main()
{
    ComplexNum complex(3, 4);
    ComplexNum c2(5, 6);
    cout << c2 << endl;
    ComplexNum& c3 = c2 + complex;
/*getting error in the below code. c3 is o/p gibberish value as if not initialized*/
    cout << c3<< " " << c2 << endl;
    return 0;
}

我正在获取乱码,好像变量c3没有初始化。

最佳答案

这里的代码会导致内存泄漏,因为一旦到达作用域的末尾,指针a就会自动删除,并且您无法删除new在堆中分配的内存。不幸的是,您在a语句中删除return之前就已取消引用它,并且您可以访问它的值,并认为一切都很好。

ComplexNum& ComplexNum::operator+(const ComplexNum& other)
{
    ComplexNum* a = new ComplexNum(real + other.getReal(), imaginary + other.getImaginary());
    return *a;
}

老实说,您可以使用= default说明符或根本不使用它们来删除您拥有的大多数代码。使用它是因为它使代码易于阅读。

用于返回类的新实例(例如+,-,*,/)的运算符不应通过引用返回。修改类的当前实例(例如=,+=,-=,*=,/=)的运算符应通过引用返回。
#include <iostream>

struct ComplexNum
{
    int real;
    int imaginary;

    ComplexNum() = default;
    ComplexNum(int x, int y) : real(x), imaginary(y)
    {;}

    friend std::ostream& operator <<(std::ostream& out, const ComplexNum& a)
    {
        out << a.real << " & " << a.imaginary << "j";
        return out;
    }

    ComplexNum operator + (const ComplexNum &other)
    {
        int r = this->real + other.real;
        int i = this->imaginary + other.imaginary;
        return ComplexNum(r,i);
    }

    ComplexNum& operator += (const ComplexNum &other)
    {
        this->real += other.real;
        this->imaginary += other.imaginary;
        return *this;
    }

    ~ComplexNum() = default;
};


int main()
{
    ComplexNum c1(3, 4);
    std::cout << c1 << std::endl;

    ComplexNum c2(5, 6);
    std::cout << c2 << std::endl;

    ComplexNum c3 = c1 + c2;
    std::cout << c3 << std::endl;

    c3 += c1;
    std::cout << c3 << std::endl;
}

结果:
3 & 4j
5 & 6j
8 & 10j
11 & 14j

在线代码示例:https://rextester.com/QNR88316

10-08 08:21
查看更多