- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在学习运算符重载。我试图在我的代码中重载 + 运算符。当我返回隐式取消引用时,输出是乱码。
如果我在返回时显式取消引用该变量,它会正常工作。问题的发生是因为我引用了一些临时变量并且它在超出范围后被销毁了。如果是这样,那么为什么显式取消引用有效?附言: 我知道我可以在没有引用的情况下返回,并且我没有遵循代码中的规则 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
关于c++ - 变量的别名在运算符重载中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56415477/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!