Practical application of class destructor

后端 未结 6 1563
孤街浪徒
孤街浪徒 2021-01-06 09:48

I\'m currently trying to learn about classes and constructors/destructors. I understand what the two do, but I\'m having a harder time with the destructors because I can\'t

6条回答
  •  悲哀的现实
    2021-01-06 09:58

    Constructors and Destructors are very important pieces of the RAII (Resource Allocation Is Initialization) idiom. Linking the acquisition of resources (files, memory, sockets, other class objects) to the lifetime of an object is a very powerful tool. When an object goes out of scope, either through normal execution or due to an exception, having the object destructor called allows the class to clean up its resources properly, without having to burden the code using an object with lots of extra finalization steps.

提交回复
热议问题