Monday, April 1, 2013

The diamond problem

Today I came across "Diamond Problem". As usual Wikipedia gives the best basic information
http://en.wikipedia.org/wiki/Diamond_problem#The_diamond_problem

 
 
Some text copied here for quick reference.

C++ by default follows each inheritance path separately, so a D object would actually contain two separate A objects, and uses of A's members have to be properly qualified. If the inheritance from A to B and the inheritance from A to C are both marked "virtual" (for example, "class B : virtual public A"), C++ takes special care to only create one A object, and uses of A's members work correctly. If virtual inheritance and nonvirtual inheritance are mixed, there is a single virtual A and a nonvirtual A for each nonvirtual inheritance path to A. C++ requires stating explicitly which parent class the feature to be used is invoked from i.e. "Worker::Human.Age". C++ does not support explicit repeated inheritance since there would be no way to qualify which superclass to use. C++ also allows a single instance of the multiple class to be created via the virtual inheritance mechanism (i.e. "Worker::Human" and "Musician::Human" will reference the same object).

For further understanding read http://www.cprogramming.com/tutorial/virtual_inheritance.html

lso, it looks like declaring one class as virtual won't work see below link
http://stackoverflow.com/questions/13752482/virtual-inheritance-one-class-enough#

No comments:

Post a Comment