COM对象的非默认构造函数 [英] Non-default constructors for COM objects
问题描述
如何在Visual C ++中为COM对象定义非默认构造函数?
How do you define a non-default constructor for a COM object in Visual C++?
这样的事情是否可能?
或者你必须构造一个默认对象, code> init(params)方法来配置它?
Or do you have to construct a default object and use an init(params)
method to configure it?
推荐答案
COM coclasses implemented in C ++不能有一个接受参数的构造函数。 CoCreateObject()函数是创建coclass实例的主要方式,没有任何方法传递参数。与IClassFactory :: CreateInstance()相同,底层方法。
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.
所以是的,不可能,你需要一个Initialize()方法。和验证它被调用的代码,E_UNEXPECTED与一个体面的IErrorInfo消息是样板。
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.
这篇关于COM对象的非默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!