• Greg (unregistered)

    Virtual inheritance is a solution to a problem you shouldn't have in the first place. There probably are some very niche problems where this is the best solution, but in general the need for virtual inheritance is an indication your class hierarchy needs looking at with a very critical eye.

  • Hmmmm 🤨 (unregistered)

    Well, it does virtually nothing...

  • (nodebb)

    Virtual inheritance is one workaround for the "diamond problem",

    It's not, as such, a workaround, but an actual solution. And unless you can redesign your class hierarchy to eliminate the diamond (e.g. in the example given, by making one or both of B or C not inherit from A), (as far as I know) virtual inheritance is not "one workaround", but, in fact, the only solution.

    But in general virtual inheritance should be avoided almost at all costs, and multiple inheritance should be mostly avoided. In C++, it's still allowed by the language, and multiple inheritance is required to do interface-implementation split development ("implements" in Java terms). Java allows at most one "extends" inheritance, and as many "implements" inheritances as you need, but C++ has only "extends" inheritance. That means that the C++ programmer must manually construct the interface classes to be completely pure-virtual with no data members, and to have no concrete implementations of any function except the destructor, which might as well, in C++11+, be ~ClassName() = default; because it has exactly nothing to do.

Leave a comment on “A Diamond in the Rough”

Log In or post as a guest

Replying to comment #:

« Return to Article