You did not define an assignment operator, so when you do p[2] = p[0] + p[1]
, the default assignment operator is used, which assigns p[2].coeff
to point to the same array as the coeff
of the temporary object created by the addition.
(您没有定义赋值运算符,因此当您执行p[2] = p[0] + p[1]
,将使用默认的赋值运算符,它将p[2].coeff
指向与数组相同的数组。加法创建的临时对象的coeff
。)
So when the temporary object is destroyed, its destructor deletes the array and p[2].coeff
is now an invalid pointer.
(因此,当临时对象被销毁时,其析构函数将删除该数组,并且p[2].coeff
现在是无效的指针。)
Therefore accessing it will lead to undefined behaviour. (因此,访问它会导致不确定的行为。)
Also a copy constructor should take a const
reference as its argument.
(同样,复制构造函数应将const
引用作为其参数。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…