1

I hope you can help me with the following problem. I am trying to create a flexible system of interfaces and hit a problem. This is the relevant code:

// Interface 1
//      this: virtual f_a
// -> abstract
struct I_A abstract
{
    virtual void f_a() = 0;
};

// Interface 2
//      I_A : virtual f_a
//      this: virtual f_b
// -> abstract
struct I_B abstract : public I_A
{
    virtual void f_b() = 0;
};

// Implementation 1
//      I_A : virtual f_a
//      zhis: defines f_a
// -> not abstract
struct S_A : public I_A
{
    virtual void f_a() {}
};

// Implementation 2
//      I_B : virtual f_a
//      I_B : virtual f_b
//      S_A : defines f_a
//      this: defines f_b
// -> not abstract
struct S_B : public I_B, public S_A
{
    virtual void f_b() {}
};

I cannot instantiate S_B because the compiler states it is abstract. What is wrong?

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
Beta Carotin
  • 1,659
  • 1
  • 10
  • 27
  • Possible duplicate of http://stackoverflow.com/questions/11662890/inherit-from-multiple-partial-implementations-of-an-abstract-base-class/11662963#11662963 – juanchopanza Aug 12 '12 at 10:55

1 Answers1

1

You need to use virtual inheritance here:

struct I_A
{
    virtual void f_a() = 0;
};

struct I_B : virtual public I_A
{
    virtual void f_b() = 0;
};


struct S_A : virtual public I_A
{
    virtual void f_a() {}
};

Note 1: I am ignoring your abstract statements in the class declarations, since it isn't standard C++.

Note 2: There is a duplicate of this here, where you can find explanations as to why this happens.

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Thank you! I can now compile the code, but I am getting the warning C4250: warning C4250: 'S_B' : inherits 'S_A::S_A::f_a' via dominance What does it mean and what can I do? – Beta Carotin Aug 12 '12 at 11:30
  • @BetaCarotin I guess it is telling you you have diamond inheritance, and it tells you where you get `f_a` from `S_A`. But I don't use VS, and gcc doesn't give me any warnings. – juanchopanza Aug 12 '12 at 11:35
  • Okay, I guess I´ll just ignore it then. Thank you for your help! – Beta Carotin Aug 12 '12 at 12:19