Virtual Inheritance and Destructors

When a derived class inherits from its base class virtually, does the destructor of the base class still have to be virtual? How about if the base class is made to not have any objects of its own? This is for something like the diamond problem.

i.e.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Parent{
protected:
    Parent(){}
    /*virtual*/ ~Parent(){}
public:
    //Maybe some variables and functions
};
class Gen_A: virtual public Parent{
protected:
    Gen_A(){}
    /*virtual*/ ~Gen_A(){}
public:
    //More members
};
class Gen_B: virtual public Parent{
protected:
    Gen_B(){}
    /*virtual*/ ~Gen_A(){}
public:
    //Even more members
};
class Final: public Gen_A, public Gen_B{
protected:
    //variables maybe
public:
    Final(){}
    ~Final(){}
    //Whatever member functions
};
If you're having a diamond in your hierarchy, it means that you have 95% to have done something wrong with said hierarchy.

You should make it virtual to avoid possible problems in future. What if you need to adapt your classes and in one moment base class becomes avaliable? What if somebody who will support this code will need of quick fix, made constructor/destructor public and after that face several different hard-to-find errors because of that?

NEVER assume that "this will never change". It leads to disasters in the future.
Last edited on
Thanks, but are you also saying that diamond hierarchies are something to be avoided?
Problem not only with diamond inheritance but with miltiple inheritance as whole.
http://stackoverflow.com/questions/225929/what-is-the-exact-problem-with-multiple-inheritance
http://javapapers.com/core-java/why-multiple-inheritance-is-not-supported-in-java/

Most of the time only legal case of multiple inheritance is interface classes.
In other cases you should rethink your class hierarhy.
Last edited on
Wow, those two articles are more concise and clear than ones I've looked up on my own.

I will definitely keep those tips in mind.
Topic archived. No new replies allowed.