How do I call the class’s destructor?

Rarely do you ever need to call the destructor explicitly. Instead, the destructor is called when an object is destroyed.

For an object like ob2 that is a local variable, it is destroyed when it goes out of scope:

int main() 
{ 
    date ob2(12); 

} // ob2.~date() is called here, automatically!

If you dynamically allocate an object using new, its destructor is called when the object is destroyed using delete. If you have a static object, its destructor is called when the program terminates (if the program terminates normally).

Unless you create something dynamically using new, you don’t need to do anything explicit to clean it up (so, for example, when ob2 is destroyed, all of its member variables, including day, are destroyed). If you create something dynamically, you need to ensure it gets destroyed when you are done with it; the best practice is to use what is called a “smart pointer” to ensure this cleanup is handled automatically.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)