开发者

C++ What is the purpose of destructors if compiler creates them implicitly?

开发者 https://www.devze.com 2023-01-18 03:56 出处:网络
Everywhere i read, if no destructor is defined the compiler creates one anyway. So what is the point of exp开发者_如何学JAVAlicitly defining one?

Everywhere i read, if no destructor is defined the compiler creates one anyway. So what is the point of exp开发者_如何学JAVAlicitly defining one?

Thank you


The compiler-provided default may not do everything you need done. For example, if you have dynamically allocated memory that needs to be deleted, you'll have to define the destructor yourself. The compiler will not do that for you.


In case you need to do something in the destructor.

For example, you might need to free memory or close a file handle.


The point of explicitly defining a C++ destructor is when the generated C++ one won't properly do the job of resource management. For example

class Example {
  MyType* m_pValue;
  Example() {
    m_pValue = new MyType();
};

Sure C++ defines a destructor here but what does this destructor do? Well really nothing. What I need it to do though is free the memory owned by the class. For that I need my own

~Example() {
  delete m_pValue;
}

Note: For a situation like this though you'd probably want to go with auto_ptr<T> or the like but I avoided it for demo purposes


So you can do explicit clean up that your instance does. If you allocate (new) memory then you must free it.


The implicit destructor might not do everything. There might have been resources like database connections, File handlers which need to be explicitly closed.


Another important case of an empty explicit destructor is when you derive from a base class. The base class should always have a virtual destructor otherwise you are were close to a lot of problems due memory leaks.


The implicit destructor will only destroy objects that are members of the class by calling their destructor. If there is any special, additional processing required on destruction of these objects the compiler will not know about it and thus not call these functions before destroying the object.

Also, if you are working with non-RAII code, you might need to release resources like memory, file handles, other OS objects etc before the object itself can be shut down cleanly.

There is also one other special case where you need to provide an implementation of a destructor, namely when you are writing a class that is designed to be a base class and thus has a virtual destructor. Once you explicitly declare the destructor (virtual or not doesn't matter), the compiler will not generate an implicit destructor. So in this case you will have to provide a definition of the destructor, even if it is empty.


Explicit constructors and destructors are very useful in implementing RAII (Resource Acquisition Is Initialization). It is quite possible to free up resources without using a destructor, but then there are ugly concerns, e.g. what will happen if an exception is thrown before the resources are freed.

Sticking resource acquiring tasks in a constructor and resource freeing tasks in a destructor is a simple way to ensure that the resources are freed up when they should be.


I use an explicit destructor in our IO class to ensure that any files (HDF, NetCDF, ASCII/binary) are properly closed. I don't think the default compiler-generated destructor will do that for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消