简体   繁体   English

类构造函数中的新功能会发生什么?

[英]What happens to new in class constructors?

This is my class definition and only copy constructor from a larger program. 这是我的类定义,只是来自较大程序的复制构造函数。 When my destructor executes, will it automatically release the 'coeff' memory? 当我的析构函数执行时,它会自动释放'coeff'内存吗? I believe that it does, however, my program throws a _CrtIsValidHeapPointer(pUserData) error after the program has been completed. 我相信它确实会在程序完成后抛出_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];
}

Thanks 谢谢

The destructor will only cout << "Destructor\\n" . 析构函数只会cout << "Destructor\\n"构函数cout << "Destructor\\n" It will not release any memory. 它不会释放任何内存。

The destructor takes care of any memory that the constructor allocated automatically, it won't do anything more. 析构函数负责构造函数自动分配的任何内存,它不会再做任何事情。 So if you allocated memory by yourself (like when you used new ), the destructor will leave it. 因此,如果你自己分配内存(就像你使用new ),析构函数就会离开它。 If it would behave differently, it could destroy data you're using in a different place of the program. 如果它的行为不同,它可能会破坏您在程序的不同位置使用的数据。 That's why it doesn't do delete on variables you created with new or new[] . 这就是为什么它不会delete你用newnew[]创建的变量。

Check your IDE documentation hovewer, because it can have a cleanup mechanism that clears all data after the program shuts, and it can embed that mechanism automatically in your output code. 请检查您的IDE文档,因为它可以有一个清除机制,在程序关闭后清除所有数据,并且它可以在输出代码中自动嵌入该机制。

Destructor will do as your code, nothing more. 析构函数将作为您的代码,仅此而已。 Your destructor just prints a message. 您的析构函数只是打印一条消息。 Try below code: 试试以下代码:

~Poly() {
    delete [] coeff; // if you allocated memory by new in constructor!
}

As a hint, try not to use bare pointers. 作为提示,尽量不要使用裸指针。 There are always std::vector , std::unique_ptr ,... 总有std::vectorstd::unique_ptr ,...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果不为具有不同签名的不同派生构造函数调用基类的构造函数,会发生什么情况? - What happens if you don't call a base class's constructor for different derived constructors with different signatures? 当使用malloc而不是new时,类成员会发生什么? - What happens to class members when malloc is used instead of new? 将新的构造函数添加到专门的模板类 - Adding new constructors to a specialised template class 函数中的类对象会发生什么 - What happens to an object of a class in a function 在类初始化期间会发生什么? - What happens during initialization of a class? 有时,该类必须“默认复制c&#39;tor”? 当我做“新类型[..]”时会发生什么? - There are times so that class must “default copy c'tor” ? and what is happens while I doing “new Type[..]”? 使用“-fno-exceptions”,“new T”会发生什么? - With “-fno-exceptions”, what happens with “new T”? 以前有关新作业的数据会怎样? - What happens to previous data on new assignment? 使用“new []”分配并仅使用“delete”删除会发生什么 - What happens on allocating with "new []" and deleting with just "delete" 如果你取消引用`new int`会发生什么? - What happens if you dereference `new int`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM