Can a C++/CX interface inherit from IClosable?

六月ゝ 毕业季﹏ 提交于 2019-12-12 05:25:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!