开发者

How/when do I use a virtual destructor?

开发者 https://www.devze.com 2023-01-29 06:56 出处:网络
I\'m currently writing a program with the following polymorphic hierarchy: Base: Multinumber. Derived: Pairs, Complex, Rational. Multinumber is a virtual class and is never instantiated.

I'm currently writing a program with the following polymorphic hierarchy: Base: Multinumber. Derived: Pairs, Complex, Rational. Multinumber is a virtual class and is never instantiated.

During the course of my program I frequently dynamically manage the base classes so I need a destructor. My question is this: how do I make the destructor virtual? In my Multinumber.h file right now I have this:

  virtual ~Multinumber();

In my Multinumber.cpp:

Multinumber::~Multinumber()
{
}

And in all of my derived classes I have this:

Rational::~Rational()
{
}

I have nothing in any of my derived.h files. Unfortun开发者_如何学Pythonately, this does not compile. Rather, I get this error:

Complex.cpp|75|error: definition of implicitly-declared 'virtual Complex::~Complex()'

What is wrong with my syntax? Thanks for any help you can give me.


Just declaring the virtual destructor in the base, and giving it the empty definition, is sufficient. For the other classes you do not need to do anything at all, unless there's actual work for those destructors to do.

The point of the virtual declaration in the base class is to ensure that the destructor can be invoked polymorphically (so that Base* d = new Derived(); delete d; works correctly, calling the Derived destructor instead of the Base destructor). You then have to define that destructor (even if it does no work) because you declared it.

However, for all of the derived classes, if you don't specify anything, the default "call destructors for members and bases" destructor gets generated for them, and everything works as you need it to. Unless, again, you need to do anything else to destruct the object properly.

As Dark Falcon noted, you need a declaration in the base for every member that you define, including destructors. So if you do write Complex::~Complex, then it must be declared in the Complex class definition, even though you inherit from a class that declares and defines a destructor. (Destructors, like constructors, aren't actually inherited anyway; the default "call recursively on members and bases" behaviour isn't really the same thing. These functions are special, since they manage the object lifetime, rather than using the object.)


Within the class Complex, you also need to have a declaration for the destructor:

~Complex();

Note that virtual is optional here. The destructor will be virtual because the base's destructor is virtual.


When to declare a destructor virtual?

I recommend you to follow this algorithm to decide whether you should declare a destructor virtual or not.

Is your class intended to be used as a base class?

  • No: Declare non-virtual destructor (avoids v-pointer on each object of the class) and remember not to derive from concrete classes.
  • Yes: Go to next question.

Is your base class abstract? (i.e. any virtual pure methods?)

  • No: Try to make you base class abstract by redesigning your hierarchy [1] (i.e. don't allow you base class to be instantiated).
  • Yes: Go to next question.

Do you want to allow polymorphic deletion though a base pointer?

  • No: Declare protected virtual destructor to avoid the unwanted usage.
  • Yes: Declare public virtual destructor (no overhead in this case).

References:

[1]: S. Meyers. More Effective C++, Item 33 (Addison-Wesley, 1996).

[2]: "Virtuality" http://www.gotw.ca/publications/mill18.htm


What is wrong with my syntax?

The following:

And in all of my derived classes I have this:

Rational::~Rational() { }

No you don't. You didn't do it for Complex.


The question is: When to not declare the destructor virtual? There are no drawbacks to declaring a destructor virtual.

0

精彩评论

暂无评论...
验证码 换一张
取 消