Access Menu

Site Navigation

« Hier werden we verliefd | Main | Dutch handicap »

February 1, 2006

Virtual destructor ~ C/C++

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.

Posted at 7:59 AM

Trackback Pings

TrackBack URL for this entry:
http://www.kiffinsblog.com/cgi-bin/mt/mt-tb.cgi/240

0 Comment(s)

Post a comment

Thanks for signing in, . Now you can comment. (sign out)

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)


Remember me?