0

I ran into a specific problem regarding virtual inheritance. The code below explains the structure.

// Header

struct ITarget
{
    virtual void SetValue() = 0;
};

struct ISource
{
    virtual void GetValue() const = 0;
};

struct IBase
{
    virtual void Stuff() = 0;
};

struct IInput :
    virtual public IBase, public ITarget
{
    // Publicly acts only as Target
};

struct IOutput :
    virtual public IBase, public ISource
{
    // Publicly acts only as Source
};

// Implementation

struct SBase :
    virtual public IBase, virtual public ISource, virtual public ITarget
{
    // IBase
    virtual void Stuff();

    // ISource
    virtual void GetValue() const;

    // ITarget
    virtual void SetValue( );
};

struct SInput :
    public IInput, public SBase
{
    // Defines ITarget and ISource per SBase
};

struct SOutput :
    public IOutput, public SBase
{
    // Defines ITarget and ISource per SBase
};

int main()
{
  // To demonstrate the behavior
  ISource* input = new SInput();
  ((SInput*)input)->GetValue();
  return 0;
}

Now, if I want to cast an ISource* to an SInput*, I get error C2635:

"cannot convert a 'ISource*' to a 'SInput*'; conversion from a virtual base class is implied"

How exactly do I have to set up the inheritance to make this possible?

Beta Carotin
  • 1,659
  • 1
  • 10
  • 27
  • 1
    What form of casting did you try? – john Nov 02 '12 at 14:20
  • 1
    Please try to post a [Short, Self Contained, Correct, Example](http://sscce.org) next time. Also try `dynamic_cast`. – Anonymous Coward Nov 02 '12 at 14:35
  • I already stripped the code down to an explanatory minimum. Also, how am I supposed to post a "Correct" example when asking for how to do it correctly? – Beta Carotin Nov 02 '12 at 14:41
  • Compiling your example produced `cannot instantiate abstract class` because at least one method implementation was missing, which isn't the problem you try to solve. Also you didn't include the line of code that actually produced the error (the c-style cast). It's a lot more convenient to help when the example provided demonstrates the actual problem out of the box. – Anonymous Coward Nov 02 '12 at 15:01
  • I think the error `cannot instantiate abstract class` also occurs because the virtual inheritance in this example is incorrect. I added the requested code to my example, thank you for your patience. – Beta Carotin Nov 02 '12 at 15:21

0 Answers0