明确新的密封方法

时间:2012-10-11 09:16:53

标签: interface c++-cli override virtual sealed

假设我有这段代码:

interface class IFoo
{
public:
   void foo();
};

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

我需要在派生类中定义一个新的显式foo(),它会覆盖基类中的sealed方法。我怎么做?我尝试了很多东西而没有编译。

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

1 个答案:

答案 0 :(得分:0)

看起来我接近答案,那是

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

请注意,如果您使用MFC项目,DEBUG_NEW正在重新定义新的,因此您将获得有关'string'的奇怪语法错误。您必须在使用此类派生类的cpp文件中注释该DEBUG_NEW宏。