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

时间:2019-06-02 13:26:22

标签: c++ dereference

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

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

1 个答案:

答案 0 :(得分:0)

此处的代码会导致内存泄漏,因为一旦到达作用域的末尾,指针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