c++ - 类构造函数中的 new 会发生什么?

标签 c++ constructor new-operator

这是我的类定义,只是从一个更大的程序中复制构造函数。当我的析构函数执行时,它会自动释放'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];
}

谢谢

最佳答案

析构函数只会cout << "Destructor\n" .它不会释放任何内存。

关于c++ - 类构造函数中的 new 会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19895845/

相关文章:

c++ - 是否可以在我的模板类中有一个静态成员变量,而类的用户不必知道它?

c++ - map/unordered_map 插入期间内存分配失败

c++ - 我正在用 placement new 做一些时髦的事情,但事情都失败了。解决方法?

c++ - 如果我将 free 与 new 一起使用或将 delete 与 malloc 一起使用,结果会怎样?

c++ - 为什么我不能在同一行中定义两个类的相同类型的成员指针

c++ - 稍后更新其中一个变量时,计算结果不会改变

c++ - std::thread 构造和执行

Java 构造函数和点运算符

c++ - 在派生构造函数中访问基本成员时出现问题

c++ - STL或Boost中是否有QList类型的数据结构?