0

My Goal: In C# I would write

    interface A
    {
        void fA();
    }
    interface B
    {
        void fB();
    }
    interface AB : A, B
    {
    }

    class SomeImplementationOfJustA : A
    {
        public void fA() { }
    }
    class ImplementationOfAB : SomeImplementationOfJustA , AB
    {
        public void fB() { }
    }

My problem: In C++ I came up with

class A
{
public:
    virtual void fA() = 0;
};
class B 
{
public:
    virtual void fB() = 0;
};

class AB : public A, public B
{
};
class SomeImplementationOfJustA : public A
{
public:
    virtual void A::fA() override { std::cout<< "fA"; }
};
class ImplementationOfAB : public SomeImplementationOfJustA , public AB
{
public:
    virtual void B::fB() override {}
};

but it does not allows me to call AB * x = new ImplementationOfAB();

I get Error C2259 'ImplementationOfAB ': cannot instantiate abstract class

In ImplementationOfAB, is there a way to take the partial implementation of A from SomeImplementationOfJustA and just let me implement the remaining partial implementation of B?

ste
  • 1
  • 1

1 Answers1

2

Its because ImplementationOfAB doesn't contains implementation of fA inherited from AB.

You can simple bypass this disadvantage by forwarding your fA method from SomeImplementationOfJustA:

class ImplementationOfAB : public SomeImplementationOfJustA, public AB
{
public:
    virtual void A::fA() override { SomeImplementationOfJustA::fA(); }
    virtual void B::fB() override {}
};
Stefan W.
  • 342
  • 1
  • 9