我遇到了一个关于虚拟继承的具体问题。下面的代码解释了结构。
// 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;
}
现在,如果我想将ISource * 转换为SInput *,我会收到错误 C2635:
“无法将 'ISource*' 转换为 'SInput*';隐含从虚拟基类的转换”
我究竟该如何设置继承才能使这成为可能?