Normally you will have to explicitly declare your own destructor if:

 

1.You are declaring a class which is supposed to serve as a base for inheritance involving polymorphism, if you do you'll need a virtual destructor to make sure that the destructor of a Derived class is called upon destroying it through a pointer/reference to Base.

 

2.You need to release resourced aquired by the class during its leftime

 

Example 1: The class has a handle to a file, this needs to be closed when the object destructs; the destructor is the perfect location.

Exempel 2: The class owns an object with dynamic-storage duration, since the lifetime of the object can potentially live on long after the class instance has been destroyed you'll need to explicitly destroy it in the destructor.

文章標籤
全站熱搜
創作者介紹
創作者 angledark0123 的頭像
angledark0123

CONY的世界

angledark0123 發表在 痞客邦 留言(0) 人氣(6)