4
class foo : public virtual bar, public virtual kung
{
  // implementation of the method of the above inherited class
};

the bar and kung class is an abstract class that contains pure virtual method that was implemented inside of the foo clas..

What is the use of this?

EdChum
  • 376,765
  • 198
  • 813
  • 562
domlao
  • 15,663
  • 34
  • 95
  • 134
  • possible duplicate of [C++ Multiple Virtual Inheritance vs. COM](http://stackoverflow.com/questions/299585/c-multiple-virtual-inheritance-vs-com) – Jason S May 08 '12 at 19:08
  • ...or this: http://stackoverflow.com/questions/4335288/virtual-inheritance-doesnt-break-static-composition – Jason S May 08 '12 at 19:10
  • possible duplicate of [In C++ virtual base class?](http://stackoverflow.com/questions/21558/in-c-virtual-base-class) – Troubadour May 08 '12 at 19:16
  • 1
    I'm sure there are **lots** of articles that talk about this. – mfontanini May 08 '12 at 19:29

3 Answers3

1

In your case it makes no difference if bar and kung are the most derived base classes so long as no methods in bar and kung clash but you would know about it if it did, ie errors from the compiler in ambiguous definitions.

More about this at c++ faq: http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.9

EdChum
  • 376,765
  • 198
  • 813
  • 562
0

If other, future classes derive from foo in more than one way, then the most-derived class will contain only one, virtual base class bar and kung:

struct A : foo { };
struct B : foo { };

class Gizmo : public A, public B { }; // only *one* bar and kung base class.

An object of type Gizmo has a unique base subobject of type bar, and ditto for kung, rather than two distinct ones for each derivation path.

Note that if a class has only pure-virtual functions and no non-static members and only base classes of the same nature, then there's no practical difference between virtual and non-virtual inheritance, since the classes are empty. Other, lesser languages call such classes "interfaces", meaning that it is OK to inherit from them multiply, even though such languages do not support multiple inheritance for general classes.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • "_then there's no practical difference_" wrong! "_since the classes are empty_" they are not; they have a vptr! – curiousguy Aug 05 '12 at 01:02
  • @curiousguy: The classes are empty nonetheless in terms of the language. There's no "vptr" in C++; that's merely an implementation detail. – Kerrek SB Aug 05 '12 at 11:14
  • "_The classes are empty nonetheless in terms of the language._" According to? "_There's no "vptr" in C++; that's merely an implementation detail._" Nitpicking. – curiousguy Aug 05 '12 at 13:33
0

By doing this, the foo class isa bar and isa kung.

If you have a function or method which takes a bar object, an instantiation of a foo class would qualify. Since the bar class has pure virtual methods, the compiler will require foo (or a class derived from foo) to define the method if the object is instantiated. The code which takes a bar object can then rely on knowing that the bar object passed to it implements that method, even if the object is an instance of the foo class (or a class derived from foo).

Likewise for the kung class.

So, if the foo class implements all the pure virtual methods in bar an kung, (which the compiler will require to instantiate a foo object) then a foo object can be passed to anything requiring a bar, a kung, or a foo object.

Marlin Pierce
  • 9,931
  • 4
  • 30
  • 52