shjeff
shjeff

Reputation: 445

Creating instance from two interface implementations

What i want is to have an object with some methods that are implemented in specific way, and other implemented different. I decided to use two interfaces. Let's call it InterfaceA and InterfaceB. Both interfaces, can have various implementations. Let me show code:

class InterfaceA
{
  public:
    virtual int method_A_foo(void) = 0;
    virtual int method_A_bar(void) = 0;
};

class InterfaceB
{
  public:
    virtual int method_B_foo(void) = 0;
    virtual int method_B_bar(void) = 0;
};

Now i have several implementations of InterfaceA, let say i have IA_instance1, IA_instance2. That implementations are base for InterfaceB which uses methods from InterfaceA, implementations:

class IA_instance1 : public InterfaceA
{
  public:
    int method_A_foo(void);
    int method_A_bar(void);
};

class IA_instance2 : public InterfaceA
{
  public:
    int method_A_foo(void);
    int method_A_bar(void);
};

class IB_instance : public InterfaceB
{
  public:

    /* in implementation of this method i want to call method_A_foo() twice */
    int method_B_foo(void);

    /* in implementation of this method i want to call method_A_foo() 4 times and method_A_bar() one time*/
    int method_B_bar(void);
};

What i have done is that i created an abstract class that wraps both interfaces. InterfaceA implementation is present in this abstract class, and InterfaceB implementation is moved to derived class:

class InterfaceBase : public InterfaceA, InterfaceB
{
  private:
    InterfaceA * m_interfaceA;

  public:
    InterfaceBase(InterfaceA * _interfaceA)
    {
      this->m_interfaceA = _interfaceA;
    }

    int method_A_foo(void)
    {
      return this->m_interfaceA->method_A_foo();
    }

    int method_A_bar(void)
    {
      return this->m_interfaceA->method_A_bar();
    }

    virtual int method_B_foo(void) = 0;
    virtual int method_B_bar(void) = 0;
};


class IB_instance1 : public InterfaceBase
{
  public:
    IB_instance1(InterfaceA * _interafceA)
      : InterfaceBase(_interafceA)
    { }

    int method_B_foo(void);
    int method_B_bar(void);
};

Now what i need is to have implementation of InterfaceA, and pass it into constructor to IB_instance1. I don't create class which implements both interfaces and thus i don't duplicate InterfaceA implementations. I use one instance of InterfaceA and i pass it to InterfaceBase wrapper. I could do following:

InterfaceA * instanceA1 = new IA_instance1();
InterfaceBase * instanceB1 = new IB_instance1(instanceA1);

I have following questions:

However, for me this seems to be good way, i think writing declarations and definitions and use separate .h and .cpp files are horrible.

Upvotes: 1

Views: 72

Answers (1)

Jarod42
Jarod42

Reputation: 217275

Need I to duplicate virtual int declarations in InterfaceBase ? If I remove them I won't be able to call InterfaceB methods after cast to InterfaceBase *. I am able to call InterfaceA methods but for InterfaceB I have to put virtual int declarations in InterfaceBase.

It is due to the fact that you privately inherit from InterfaceB instead of publicly. Change your code to

class InterfaceBase : public InterfaceA, public InterfaceB
{
    /* Your implementation without repeating InterfaceB */
};

Upvotes: 2

Related Questions