Alex Rivera | Logout

Are virtual destructors inherited?

Asked 2010-02-04T09:00:55.000
90

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 it has nothing to do?
  3. What's the best practice for declaring the derived destructor? Declare it virtual, non-virtual or omit it if possible?
Edit
Report

1 Answer

2

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.

answered 2010-02-04T09:04:25.287

Your Answer