explicitly new a sealed method
-
06-07-2021 - |
Question
Let's say I have this code:
interface class IFoo
{
public:
void foo();
};
ref class FooBase : public IFoo
{
public:
virtual void foo() sealed = IFoo::foo
{
}
};
I need to define a new explicit foo() in a derived class, that overrides the sealed method in the base class. How do I do that? I tried a lot of things and none compiled.
ref class FooDerived : public FooBase
{
public:
virtual void foo()
{
}
};
results in
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
but if I add new
ref class FooDerived : public FooBase
{
public:
virtual void foo() new
{
}
};
I get
Dlg.cpp(30) : error C2059: syntax error : 'string'
Dlg.cpp(31) : error C2091: function returns function
also
ref class FooDerived : public FooBase
{
public:
virtual void foo() new = FooBase::foo
{
}
};
results in
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'
and
ref class FooDerived : public FooBase, public IFoo
{
public:
virtual void foo() new = IFoo::foo
{
}
};
generates
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'
What I'm trying to do is overriding HwndSource.System.Windows.Interop.IKeyboardInputSink.TabInto.
Solution
Looks like I was close to the answer, which was
ref class FooDerived : public FooBase
{
public:
virtual void foo() new = IFoo::foo
{
}
};
Be aware, if you work with an MFC project, DEBUG_NEW is redefining new, and therefore you'll get strange syntax errors about 'string'. You have to comment that DEBUG_NEW macro in the cpp files where you use such a derived class.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow