3

I am trying to do something I haven't really done before. I basically have 3 classes. Class A is an abstract class with pure virtual methods, Class B is a class on it's own that contains methods with the same name as the virtual methods in Class A. I'm trying to tie everything together in Class C. I'd like to inherit class B and A in C (multiple inheritance), and use the methods from Class B to implement those in class A. This way I create a modular approach. The example below is a very simplified version of my code.

class A {
virtual int methodA() = 0;
virtual int methodB() = 0;
virtual int methodC() = 0;
};

class B { //B implements A.A() and A.B()
int methodA() { return 0; }; 
int methodB() { return 0; };
};

class C : A, B {
int methodC() { return 0; }; //C implements A.C()
};

I can compile class C but when I try construct a class of C new C() I get a compiler message saying "cannot instantiate abstract class due to following members: int methodA()' : is abstract".

Is there a way to implement class A using class B through multiple inheritance in class C?

Edit: The wish is to keep class B concrete.

  • What seems to be the problem with having `B` implement the methods directly? – Mark B Aug 22 '13 at 19:51
  • @MarkB I want B to be reusable. Class A contains a method that is implemented in class C. –  Aug 22 '13 at 19:53
  • If you really want to do this, you can implement a dummy method for B. The only reason I say, "if you really want to" is because this doesn't have very good "code smell." Maybe you can explain a little more about the real problem you're trying to solve and someone here can give you an alterate implementation. – DubiousPusher Aug 22 '13 at 20:26
  • I'm not trying to dis you btw. It's just odd to have B partially implement the interface of A and then have C complete it. If you think about an interface as a contract with the client code that uses it, B is breaking a fairly major promise to the client code. Namely that it has some operation methodC. In the case that it must be implemented this way B should at least have some "null" effect for methodC. – DubiousPusher Aug 22 '13 at 20:28
  • @DubiousPusher In the comment to TemplateRex below I have an interface which is implemented by class B but has a strict requirement to inherit an interface `IUnknown` which contains abstract methods that are standard and must be implemnted. To modularize my code I want to create a class like class B that was concrete and could be reused in any code source going forth in the future. Besides the interface IUnknown is defined in a interface definition language file and I do not think an c++ style definition exist for it. –  Aug 22 '13 at 20:36
  • @DubiousPusher, you're correct if `B` were derived from `A`, but it isn't - it has no requirement to fulfill the contract of `A`. I think we're talking about a mix-in class which is intended to implement some specific behaviors on behalf of another class, see e.g. http://stackoverflow.com/questions/7085265/what-is-c-mixin-style – Mark Ransom Aug 23 '13 at 00:55

1 Answers1

-1

You can do it with some method-by-method boilerplate.

class A {
public:
virtual int methodA() = 0;
virtual int methodB() = 0;
virtual int methodC() = 0;
};

class B { //B implements A.A() and A.B()
public:
int methodA() { return 0; }; 
int methodB() { return 0; };
};

class C : public A, public B {
public:
int methodA() { return B::methodA(); }
int methodB() { return B::methodB(); }
int methodC() { return 0; }; //C implements A.C()
};

Demo: http://ideone.com/XDKDW9

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • `B` is not meant to be a base class (no virtual destructor for starters) – TemplateRex Aug 22 '13 at 22:39
  • 1
    @TemplateRex I think you could fix that just by using private inheritance for `B`. – Mark Ransom Aug 22 '13 at 22:43
  • I think the OP's general design is just totally fubar, and saving retyping the implementation of 2 methods by abusing inheritance is just bad practice. – TemplateRex Aug 22 '13 at 22:44
  • @TemplateRex I wasn't saying this was necessarily a good idea; it would be cleaner if the implementation class used different method names. But the idea of a mixin class can be very useful. – Mark Ransom Aug 22 '13 at 22:53