0

假设我有这个代码:

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

4

1 回答 1

0

看起来我已经接近答案了,那就是

ref class FooDerived : public FooBase
{
public:
   virtual void foo() new = IFoo::foo
   {
   }
};

请注意,如果您使用 MFC 项目,则 DEBUG_NEW 正在重新定义新的,因此您会收到关于“字符串”的奇怪语法错误。您必须在使用此类派生类的 cpp 文件中注释 DEBUG_NEW 宏。

于 2012-10-11T20:13:24.453 回答