Virtual destructor

 | C/C++ | 0 Comments | 0 TrackBacks

So why the heck would you or anyone else ever want to define a virtual destructor? Have a look at the following code:

class Blah {
public:
    virtual ~Blah() {}
// more code
};

Note here that the body of the virtual destructor is empty. What gives?

When you think about it more closely, you cannot help but realize that this is necessary. Whenever a pointer to the Blah class is used to delete an object that might actually be a derived-class object, then be careful, e.g. a virtual destructor is required unless you enjoy blowing up your program in unpredictable ways.

Since this destructor is inherited by the derived classes there is no need to redefine this destructor in the derived classes.

No TrackBacks

TrackBack URL: http://www.kiffingish.com/cgi-bin/mt/mt-tb.cgi/127

Leave a comment

Recent Entries

No more winter greens
The cool, windy and grayish afternoon calls me onward. It's drizzling on my windshield but I do not care.
Champion of Scotland
I nearly cried my eyes out when I discovered that young Tommy Morris dies in the prime of his
Sticky keyboard
Up until yesterday the Del-, Ins- and Home- keys on the upper right-hand corner were sticking alot, and it's
Perl on steroids
Read this from beginning to end, and you then tell me with a straight face that it hasn't completely
Seventh Dutch Perl Workshop
Going to attend the 7th Dutch Perl Workshop tomorrow in Arnhem. Cannot wait to get up bright and early