Virtual destructors

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

Hi,

should destructors of a base class be always declared as "virtual"
in order to avoid "partially destroying"objects when a derived class
object is deleted through a pointer to a base class?

Or are there any cases where the base class destructor should
remain "non-virtual"?

Thank you.
Chris
 
R

Rolf Magnus

Christian said:
Hi,

should destructors of a base class be always declared as "virtual"
in order to avoid "partially destroying"objects when a derived class
object is deleted through a pointer to a base class?

Or are there any cases where the base class destructor should
remain "non-virtual"?

if there is no chance the object will get destroyed polymorphically, there
is basically no need for a virtual destructor. Otherwise, you must have
one.
 
R

Ron Natalie

Rolf said:
if there is no chance the object will get destroyed polymorphically, there
is basically no need for a virtual destructor. Otherwise, you must have
one.

Correct, and a virtual destructor doesn't cost anything really except
in the case when there are NO other virtual functions (i.e., the class
isn't otherwise polymorphic). A good rule of thumb, is if you have
any virtual functions, make the destructor also virtual.
 
J

Joe Van Dyk

Rolf said:
Christian Christmann wrote:




if there is no chance the object will get destroyed polymorphically, there
is basically no need for a virtual destructor. Otherwise, you must have
one.

If there's no chance for polymorphic destruction, then would you want
the destructor private?

Joe
 
I

Ian Collins

Joe said:
If there's no chance for polymorphic destruction, then would you want
the destructor private?
No, that would prevent you from instantiating the object. Both
constructor and destructor have to be public, unless you are building a
singleton.
 
R

Ron Natalie

Joe said:
If there's no chance for polymorphic destruction, then would you want
the destructor private?
Then you couldn't destroy it externally. For example:

class X {
~X(); // private desturctor
};

int main() {
X x;
}
wouldn't even compile.
 
R

Ron Natalie

Ian said:
No, that would prevent you from instantiating the object. Both
constructor and destructor have to be public, unless you are building a
singleton.
Well you could instantiate it, but you could "deinstantiate" it trhough
any method that couldn't see the destructor.

class X {
~X(); // private dstructor
public:
void DeleteMe() { delete this; }
};

int main() {
X x; // ill-formed
X* xp = new X; // OK
delete xp; // ill-formed
xp->DeleteMe(); // OK
}
 
R

roods

Rolf said:
if there is no chance the object will get destroyed polymorphically, there
is basically no need for a virtual destructor. Otherwise, you must have
one.

The only time I could see when that is possible is if the class isn't a
base class. Otherwise, assumptions about your clients seem like a bad
idea. It may not be that you are treating objects polymorphically it
may just be you are creating an abstract class in which case a virtual
dtor is also a good idea.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,600
Members
45,179
Latest member
pkhumanis73
Top