I need to write a coding convention that will be used both by newbies and experienced C++ developers. The rule on inheritance for dynamic polymorphism goes like this:
- For dynamic polymorphism, consider using single inheritance (tree-like hierarchy), possibly with multiple inheritance of abstract interfaces
- for inheritance along the hierarchy (base classes, etc.), by default, use public inheritance
- for inheritance of abstract interface, by default, use public virtual inheritance
This rule will be followed by a detailed information about implementation, possible exceptions, etc.
So, the question: Is this rule desirable for both newbies and experienced C++ developers? (pros/cons, as well as sources and links are welcome)
The ones I see are:
Note: I've read the following online sources:
Note 2: The use of the "abstract interface" name is coined after Sutter & Alexandrescu's use in item 36 of "C++ Coding Standards"
This is one case that should work (its Java/C# equivalent using interfaces just work), but that doesn't in C++ if the interface inheritance is not virtual:
class A
{
public :
virtual ~A() = 0 {}
} ;
class B : public A {} ; // should have been virtual to avoid the error
class C : public A {} ; // should have been virtual to avoid the error
class D : public B, public C
{
public :
virtual ~D() {}
} ;
void foo(A * c) {}
void bar(D * d)
{
foo(d) ; // Error: ambiguous conversions from 'D *' to 'A *
}
And yes, explicit casting to remove the ambiguity is the wrong solution (explicit casting is usually the wrong solution anyway).
Virtual inheritance is used to solve the DDD problem (Dreadful Diamond on Derivation). Look at the following example, where you have two classes that inherit from the same base class: class C : public Base { public: //... }; class D : public Base { public: //... };
Without virtual inheritance, if classes A and B both inherit from class X, and class C inherits from classes A and B, then class C will contain two copies of X's member variables: one via A, and one via B. These will be accessible independently, using scope resolution.
In C++, this might mean private inheritance or virtual inheritance. Today we speak about the latter. What is virtual inheritance? Virtual inheritance is a C++ technique that ensures that only one copy of a base class’s member variables are inherited by second-level derivatives (a.k.a. grandchild derived classes).
In other words, the virtual base class will be the last object destroyed, because it is the first object that is fully constructed. A powerful technique that arises from using virtual inheritance is to delegate a method from a class in another class by using a common abstract base class. This is also called cross delegation.
You know what? You already give all the important info in the question. I don't see anything to answer on the technical level. And apparently noone else saw any significant technical problem with what you posted either.
I'll answer your bolded question though: Yes, it is suitable for both newbs and pros.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With