这是我的类定义,仅从更大的程序中复制构造函数。当我的析构函数执行时,它会自动释放“coeff”内存吗?我相信确实如此,但是,我的程序在程序完成后会引发 _CrtIsValidHeapPointer(pUserData) 错误。
class Poly
{
private:
int order; //order of the polynomial
int size; //order + 1
int * coeff;//pointer to array of coeff on the heap
public:
Poly();
Poly(int Order);
Poly(int Order, int * Coeff);
~Poly(){cout << "Destructor\n";};
Poly(const Poly &rhs);
//accessors & mutators
void set();
void set(int * Coeff, int Order);
int getorder(){return order;};
int * get()const{return coeff;};
//Overloaded Operators
Poly operator+(const Poly &rhs);
Poly operator-(const Poly &rhs);
Poly operator*(const int scale);
Poly operator=(const Poly &rhs);
Poly operator*(const Poly &rhs);
const int& operator[](int I)const;
int& operator[](int I);
bool operator==(const Poly &rhs);
int operator( )(int X);
friend ostream & operator<<(ostream & Out, const Poly &rhs);
friend istream & operator >>(istream & In, Poly &rhs);
};
Poly::Poly(const Poly &rhs)
{
order = rhs.order;
size = rhs.size;
int *coeff = new int[size];
for(int i(0); i <= order; i++)
coeff[i] = rhs.coeff[i];
}
谢谢