If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: virtual destructor

If you have a class with virtual function then the virtual destructor is required. Otherwise normal destructor is sufficient.

That's a common rule of thumb but it's a little bit too limiting (and it's not quite accurate).

You only need a virtual destructor in a base class if you plan to use delete on a base class pointer that in fact holds a pointer to a derived class object (a polymorphic delete). If you only ever delete derived class pointers directly then the base class destructor needs not be virtual. To enforce that you could make the base class destructor protected.

So a more complete rule of thumb would be: Make base class destructors public and virtual, or protected and nonvirtual. (See item 50 in C++ Coding Standards by Sutter & Alexandrescu for a discussion.)

Re: virtual destructor

Originally Posted by Rajesh1978

If you have a class with virtual function then the virtual destructor is required. Otherwise normal destructor is sufficient.

This isn't correct, the 2 are unrelated.

the only NEED for a virtual destructor is if you plan to delete a derived object through a base pointer AND this derived class has a need for additional destructor vode or has additional data members that have destructors that need to be called.

it is entirely possible to have a class with no virtual functions at all, but still needing a virtual destructor to function properly.

I will "agree" to the fact that a class which has virtual functions, it is "probably a good idea" to make the destructor virtual also.

Re: virtual destructor

Originally Posted by OReubens

the only NEED for a virtual destructor is if you plan to delete a derived object through a base pointer AND this derived class has a need for additional destructor vode or has additional data members that have destructors that need to be called.

Re: virtual destructor

Originally Posted by laserlight

The subclause following the "AND" should be discarded.

Hmm not quite...
If the derived class does not have additional datamembers, and doesn't do anything to existing datamembers (or globals) that need to be reversed, then the derived class doesn't need a destructor at all.

I'm not sure if this is 100% "by the book", but the above rule works for every single c++ compiler (even some very oddball ones) on every single platform tested so far.

Re: virtual destructor

Originally Posted by OReubens

Hmm not quite...
If the derived class does not have additional datamembers, and doesn't do anything to existing datamembers (or globals) that need to be reversed, then the derived class doesn't need a destructor at all.

I'm not sure if this is 100% "by the book", but the above rule works for every single c++ compiler (even some very oddball ones) on every single platform tested so far.

That assumes that the designer of the base class knows the implementations of the classes that will be derived from it, and that's a bad assumption to make. There's no reason not to make a destructor virtual.

Re: virtual destructor

Originally Posted by OReubens

If the derived class does not have additional datamembers, and doesn't do anything to existing datamembers (or globals) that need to be reversed, then the derived class doesn't need a destructor at all.

I'm not sure if this is 100% "by the book", but the above rule works for every single c++ compiler (even some very oddball ones) on every single platform tested so far.

The 100% by the book rule says that there is undefined behaviour, even if it "works for every single c++ compiler" in existence and in history. But yeah, practically speaking, I believe that you are right. However, because of the theoretical undefined behaviour, the fact that the "100% by the book" rule is so much simpler and hence easier to remember, and the point that GCDEF raised, the subclause following the "AND" should be discarded.

Re: virtual destructor

In any case, your sentence should be eventually corrected to

the only NEED for a virtual destructor is if you plan to delete a derived object through a base pointer AND this derived class has a need for additional destructor code or deallocation functions ( including the base itself ) or has additional data members or base classes that have destructors that need to be called.

BTW, note that a recent proposal asked for an optionally sized global deallocation function in order to support efficient size-aware allocation strategies; if accepted, it would break such illegal code ( the deallocation function would not be given the correct size of the object to delete ).