Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual inheritance use

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:

Pros:

  • rule easily usable by newbies, without restricting experienced developpers.
  • familiar to those already familiar with Java/.NET's interfaces
  • dodges the problems related with virtual inheritance of implementation (as it is reserved for abstract interfaces), as well as non-virtual inheritance (possible ambiguity when casting to the interface class)

Cons:

  • slight performance cost (speed when casting to the interface, size of the virtual tables, additional pointer(s) in class instance)

Note: I've read the following online sources:

  • When virtual inheritance IS a good design?
  • http://www.parashift.com/c++-faq-lite/virtual-inheritance-abcs.html
  • http://www.artima.com/intv/abcs.html
  • http://cpptips.com/virt_inher2
  • http://g.oswego.edu/dl/mood/C++AsIDL.html

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).

like image 390
paercebal Avatar asked Dec 02 '13 12:12

paercebal


People also ask

What is virtual inheritance in Java?

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: //... };

What happens if a class does not have virtual inheritance?

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.

What is private inheritance or virtual inheritance in C++?

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).

What is the last object destroyed in virtual inheritance?

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.


1 Answers

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.

  • Newbs have some useful technical guidelines.
  • Pros can do what they want if they can give a rationale, because you qualify your rules with "consider" and "by default", so basically noone else can say you have to do so or so because of the style rules, because your rules' phrasing already allow for exceptions.
like image 190
Martin Ba Avatar answered Oct 15 '22 11:10

Martin Ba