views:

222

answers:

4

If I have a base class with a virtual destructor. Has a derived class to declare a virtual destructor too?

class base {
public:
    virtual ~base () {}
};

class derived : base {
public:
    virtual ~derived () {} // 1)
    ~derived () {}  // 2)
};

Concrete questions:

  1. Is 1) and 2) the same? Is 2) automatically virtual because of its base or does it "stop" the virtualness?
  2. Can the derived destructor be omitted if he has nothing to do?
  3. What's the best practise for declaring the derived destructor? Declare it virtual, non-virtual or omit it if possible?
+9  A: 
  1. Yes, they are the same. The derived class not declaring something virtual does not stop it from being virtual. There is, in fact, no way to stop any method (destructor included) from being virtual in a derived class if it was virtual in a base class.
  2. Yes, a destructor in a derived class can be omitted if it has nothing to do. And it doesn't matter whether or not its virtual.
  3. I would omit it if possible. And I always use the virtual keyword again for virtual functions in derived classes for reasons of clarity. People shouldn't have to go all the way up the inheritance hierarchy to figure out that a function is virtual.

As a small point for item 3. It has been pointed out in comments that if a destructor is undeclared the compiler generates a default one (that is still virtual). And that default one is an inline function.

Inline functions potentially expose more of your program to changes in other parts of your program and make binary compatibility for shared libraries tricky. Also, the increased coupling can result in a lot of recompilation in the face of certain kinds of changs. For example, if you decide you really do want an implementation for your virtual destructor then every piece of code that called it will need to be recompiled. Whereas if you had declared it in the class body and then defined it empty in a .cpp file you would be fine changing it without recompiling.

My personal choice would still be to omit it when possible. In my opinion it clutters up the code, and the compiler can sometimes do slightly more efficient things with a default implementation over an empty one. But there are constraints you may be under that make that a poor choice.

Omnifarious
Your last sentence should probably read "shouldn't" instead of "should."
Chris Lutz
@Chris Lutz, I'm ahead of you on that. It's been edited into submission now. :-)
Omnifarious
I disagree with the 'omit' part. It does not cost much to declare it in the header and define it (empty body) in the source. If you do so, you can always come back and add some steps (logging ?) without forcing your clients to recompile.
Matthieu M.
@Matthieu M, that's an argument for not declaring any function inline. Though I will grant that it's likely a little more common to want to log constructor and destructor calls than anything else.
Omnifarious
Actually, I don't declare much function inline, not even the classic 'accessors', but then working in a big company, we may have binary compatibility constraints that are a higher than most.
Matthieu M.
+1  A: 

A virtual member function will make implicitely any overloading of this function virtual.

So the virtual in 1) is "optional", the base class destructor being virtual makes all child destructors virtual too.

Klaim
A: 
  1. The destructor is automatically virtual, as with all methods. You can't stop a method from being virtual in C++ (if it has already been declared virtual, that is, i.e. there's no equivalent of 'final' in Java)
  2. Yes it can be omitted.
  3. I would declare a virtual destructor if I intend for this class to be subclassed, no matter if it's subclassing another class or not, I also prefer to keep declaring methods virtual, even though it's not needed. This will keep subclasses working, should you ever decide to remove the inheritance. But I suppose this is just a matter of style.
roe
Destructors are not automatically virtual, and neither are any other member functions.
anon
@Neil; of course not, I was referring to _the_ destructor in the example (i.e. where the base class has a virtual one), not destructors in general. And this is true for all methods, not just destructors.
roe
A: 

1/ Yes 2/ Yes, it will be generated by the compiler 3/ The choice between declaring it virtual or not should follow your convention for overriden virtual members -- IMHO, there are good arguments both way, just choose one and follow it.

I'd omit it if possible, but there is one thing which may incite you to declare it: if you use the compiler generated one, it is implicitly inline. There are time when you want to avoid inline members (dynamic libraries for instance).

AProgrammer