假设我有这个代码:
interface class IFoo
{
public:
void foo();
};
ref class FooBase : public IFoo
{
public:
virtual void foo() sealed = IFoo::foo
{
}
};
我需要在派生类中定义一个新的显式 foo(),它会覆盖基类中的密封方法。我怎么做?我尝试了很多东西,但都没有编译。
ref class FooDerived : public FooBase
{
public:
virtual void foo()
{
}
};
结果是
error C4485: 'FooDerived::foo' : matches base ref class method 'FooBase::foo', but is not marked 'new' or 'override'; 'new' (and 'virtual') is assumed
1> .\Dlg.cpp(22) : see declaration of 'FooBase::foo'
1> Specify 'override' (and 'virtual') to override the ref class virtual method
1> Specify 'new' (and 'virtual') to hide the ref class virtual method with a new virtual method
1> Position for 'new' and 'override' keywords is after method parameter list
但是如果我添加新的
ref class FooDerived : public FooBase
{
public:
virtual void foo() new
{
}
};
我明白了
Dlg.cpp(30) : error C2059: syntax error : 'string'
Dlg.cpp(31) : error C2091: function returns function
还
ref class FooDerived : public FooBase
{
public:
virtual void foo() new = FooBase::foo
{
}
};
结果是
1>.\Dlg.cpp(30) : error C2059: syntax error : 'string'
1>.\Dlg.cpp(30) : error C2091: function returns function
1>.\Dlg.cpp(31) : warning C4569: 'FooBase::foo' : no members match the signature of the explicit override
1>.\Dlg.cpp(31) : error C3671: 'FooDerived::foo' : function does not override 'FooBase::foo'
和
ref class FooDerived : public FooBase, public IFoo
{
public:
virtual void foo() new = IFoo::foo
{
}
};
生成
1>.\Dlg.cpp(30) : error C2059: syntax error : 'string'
1>.\Dlg.cpp(30) : error C2091: function returns function
1>.\Dlg.cpp(31) : warning C4569: 'IFoo::foo' : no members match the signature of the explicit override
1>.\Dlg.cpp(31) : error C3671: 'FooDerived::foo' : function does not override 'IFoo::foo'
我想要做的是覆盖HwndSource.System.Windows.Interop.IKeyboardInputSink.TabInto。