问题
How does one declare a runtime interface that inherits from Windows.Foundation.IClosable
in C++/CX?
Both of these attempts produce compiler errors:
public interface class MyInterface : Windows::Foundation::IClosable {
/* bla bla */
};
==> error C2039: 'IClosable' : is not a member of 'Windows::Foundation'
and
public interface class MyInterface {
/* bla bla */
~MyInterface();
};
==> error C2849: 'MyInterface' : an interface cannot have a destructor
Yet it cannot be that such inheritance is categorically forbidden in Windows Runtime, because some system-provided interfaces do inherit from IClosable
-- for example, IInputStream
.
I suppose I could define MyInterface
in IDL instead and convert it into an external .winmd
file with midlrt.exe before I compile the C++ code. That would be an unwelcome compilcation of my build process, though. Is there a way to specify this as C++/CX source?
回答1:
You can inherit from IClosable only if you do not use C++/CX.
The reason why you get the first error is because this interface is not a part of Windows.winmd metadata. This interface is declared in windows.foundation.h file ABI::Windows::Foundation namespace.
The second error appears because an interface cannot have a destructor.
MSDN: When you declare a public destructor, the compiler generates the code so that the ref class implements Platform::IDisposable and the destructor implements the Dispose method. Platform::IDisposable is the C++/CX projection of Windows::Foundation::IClosable. Never explicitly implement these interfaces.
来源:https://stackoverflow.com/questions/14594171/can-a-c-cx-interface-inherit-from-iclosable