Non-default constructors for COM objects

大憨熊 提交于 2019-12-12 02:18:39

问题


How do you define a non-default constructor for a COM object in Visual C++?

Is such a thing even possible?

Or do you have to construct a default object and use an init(params) method to configure it?


回答1:


COM coclasses implemented in C++ cannot have a constructor that takes an argument. The CoCreateObject() function, the primary way to create an instance of a coclass, doesn't have any way to pass arguments. Same with IClassFactory::CreateInstance(), the underlying method.

So yes, not possible, you'll need an Initialize() method. And the code to verify that it was called, E_UNEXPECTED with a decent IErrorInfo message is boilerplate.



来源:https://stackoverflow.com/questions/8521511/non-default-constructors-for-com-objects

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