变量的别名在运算符重载中不起作用

Alias of variable not working in operator overloading

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

如果我在 returning 时显式取消引用变量,它会正常工作。 问题的发生是因为我引用了一些临时变量并且它在超出范围后被销毁了。如果是这样,那么为什么显式取消引用有效? P.S。 : 我知道我可以 return 没有参考而且我没有遵循代码中的规则 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 在堆中分配的内存.不幸的是,您在 return 语句中删除 a 之前取消引用它,并且您能够访问它的值,认为一切正常。

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

老实说,您拥有的大部分代码都可以通过使用 = default 说明符或根本不使用它们来省略。使用它是因为它使代码易于阅读。

用于 return class 新实例的运算符(例如 +,-,*,/)不应通过引用 return 编辑。修改 class 的当前实例的运算符(例如 =,+=,-=,*=,/=),应该通过引用 return 编辑。

#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