   Trouble with multiple inheritance and abstract classes
  Right, I know it might be best not to mess with multiple inheritance at all (at least that's what I've been told by several people who're in the know), but for my specific application, multiple inheritance seems the best way to go. Well, that's how it was until I ran into the following problem.Let's say we have an abstract base class:    class Base{protected:    virtual void func() = 0;};From this class we derived two new classes. One (class Derived_abstr  ) adds some more functionality but doesn't implement func()  , hence remaining an abstract class, just like class Base  . The other one (class Derived_impl  )actually implements func()   and is therefore not abstract. Like so: /* uses virtual inheritance because I don't want 'Base' to end being contained in the user-end class more than once */class Derived_abstr : public virtual Base{protected:    /* ... adds some other functions, but does __NOT__ implement   func() */};class Derived_impl : public virtual Base{protected:    /* actually implements func() */    virtual void func() { ..... }};So far so good. Next I'm going to make a class CombinedClass, which is derived both from class Derived_abstr and from class Derived_impl.  class CombinedClass : public Derived_abstr, public Derived_impl{protected:    /* do some other stuff. do __not__ reimplement func() */    ...};What I would expect is that CombinedClass  inherits the implemented version of func()  from class Derived_impl , and indeed this seems to be the case, as I get a warning from the compiler saying that CombinedClass  "inherits func  from Derived_impl  via dominance". Well, if that is so, then how come I can't create instances of class CombinedClass?? Because when I try, I get yelled at for trying to instantiate an abstract class! Oh, and the other thing I want to ask, are these inheritance via dominance warnings a bad thing? Because I figured that that is in fact the kind of behavior I _want_ from class hierarchy.Thanks in advance for any helpful replies![edited by - Red Ant on February 16, 2004 4:32:42 AM][edited by - Red Ant on February 16, 2004 4:34:50 AM]
  I''m about to go to sleep, so I''ll just quickly give it a shot.Try: class CombinedClass : public Derived_abstr, public Derived_impl{  using Derived_impl::func();protected:/* do some other stuff.do __not__ reimplement func() */...};That should remove the warning, but as for the pure virtual function problem, *shrug*. In doubt go ask on comp.lang.c++.moderated.   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor);  well, you just figured out why people will tell you to stay away from it unless you absolutely have to. not only will you inherit a whole lot of stuff twice (everything thats in the base class), but now you got two different functions with the same name, one is abstract and one isnt. you still need to implement the abstract class because the compiler wont say "oh, they happen to have the same signature and name, so i''ll just remove one of them". it cant, it shouldnt, it mustnt, because both functions having the same name could be coincidence. also, afaik it wont trace back your inheritance tree and remove duplicate members and methods. avoid that diamond setup, use interfaces instead or at least keep it "clean" (ie, make the base class empty if you can). you already defeated the purpose of virtual functions, because when you now call combined_instance.func() it cant tell which of the many versions of func to call.in other words: implement that function in your combined class if the above doesnt work and address the inherited version as Derived_impl::func... welcome to the ugly and messy world of multiple inheritance. ;  Okay, I see what you're saying in regards to the compiler not being able to figure out whose version of func() my class should inherit, but doesn't virtual inheritance mean that you don't    end up having several copies of the base class encapsulated in the 'user-end class' (can't think of a better word right now)?? And I would love to ditch the whole multiple inheritance approach, but I can't figure out another way that would achieve the same thing for me, so I'm afraid I'm stuck with it until the lightning of inspiration strikes me.   [edited by - Red Ant on February 16, 2004 7:39:12 AM];  I tried your code in VC++ 7.1 and it compiles and works as intended by you. ;  Yeah, it does. I''m such a knobhead, and I should be shot for making the 22nd mistake caused by c&p this week! :oIn the piece of code that actually used for testing, I simply copied the pure virtual declaration of func() from class Base to class Derived_impl. Oh man ... 