views:

207

answers:

4

I hope this is a simple question.

Can I inherit both an abstract class and it's implementation? That is, can the following be made to work?

class A { 
   virtual void func1() = 0; 
}

class B {
   void func1() { /* implementation here */ }
}

class C : public A, public B {
}

I've tried a few variations, and am getting compile errors complaining about unimplemented methods in class C. I can save a lot of repeated code if I can make this work, however. Is it possible?


I solved this by creating a "composite class" called D which inherits from A & B, but contains the implementation code previously contained in B. This makes my inheritance model less clean, but it solves the problem without requiring code duplication. And, as I noted in the comments below, it makes my naming conventions pretty gross.

class A { 
   virtual void func1() = 0; 
}

class B {
   // Other stuff
}

class D : public A, public B {
   void func1() { /* implementation here */ }
}

class C : public D {
}
+1  A: 

Make B inherit from A. If that is not possible, using virtual inheritance might also work (I am not entirely sure about this).

Tronic
In this case, A and B are very different classes, and really have nothing in common besides a small number of functions.
Watusimoto
When you use non-virtual inheritance, C gets two entirely separate func1 functions.
Tronic
it would be pretty hack if B didn't inherit from A and you were trying to get it to override A's function by kinda mashing them together in Calso Diamond inheritance would still require B to inherit from A, it would have to be:class B: virtual Aclass C: virtual A, B
matt
That "pretty hack" is exactly what I wanted to do.
Watusimoto
+1  A: 

If you want to reuse code from B class in C class, try to do something like this:

class C : public A, public B {
void func1(){ B::func1(); }
}
Miollnyr
+2  A: 

class B is not an implementation of class A in your code. Class B should be inherited from class A and func1 should be virtual. Only in that case class B will be implementation of class A. And then there is no need to inherit from both A and B.

class A { 
   virtual void func1() = 0; 
}

class B : public A {
   virtual void func1() { /* implementation is here */ }
}

class C : public B {
}

Otherwise you will get unimplemented pure virtual function func1.

Kirill V. Lyadvinsky
A: 

As Kirill pointed out: Your premise is wrong.

Class B in your example does not inherit class A (it needs to be declared to do that first).

Thus, B.func1() is something entirely different to A.func1() for the compiler. In class C it is expecting you to provide an implementation of A.func1()

Somebody above posted something along the lines of:

class C : public A, public B 
{
      // implement A::func1()
      virtual void func1()
      { 
           // delegate to inherited func1() in class B
           B::func1(); 
      }
}
Daren Thomas
Well, I guess we've narrowed the problem to one of finding a good naming convention:class LuaPolygonalGameObject : public GameObject, public Polygon, public LuaItem // Composite classyuck! But it does compile!
Watusimoto