gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:35:49 25 4
gpt4 key购买 nike

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

如果我在返回时显式取消引用该变量,它会正常工作。问题的发生是因为我引用了一些临时变量并且它在超出范围后被销毁了。如果是这样,那么为什么显式取消引用有效?附言: 我知道我可以在没有引用的情况下返回,并且我没有遵循代码中的规则 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 说明符或根本不使用它们来省略。使用它是因为它使代码易于阅读。

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

#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

关于c++ - 变量的别名在运算符重载中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56415477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com