Deconstructors

Posted on April 18, 2013. Filed under: C++ | Tags: |


When a c++ object get destroyed (at the finishing of a program), it automatically calls a deconstructor to do stuff like house keeping.
The syntax of creating a deconstructor is ~Whateverclassname();
The deconstructor have no return type, no parameters and can not be overloaded. (c++ constructor have no return type, can have parameters and can be overloaded)

Note:

Compare to C++, Java have no destructor. The reason is that all Java objects are heap allocated and garbage collected. Without explicit deallocation (i.e. C++’s delete operator) there is no sensible way to implement real destructors. There is an inherited method called finalize, but this is called entirely at the discretion of the garbage collector. So for classes that need to explicitly tidy up, the convention is to define a close method and use finalize only for sanity checking (i.e. if close has not been called do it now and log an error).

Advertisements

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: