0

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?

Sideshow Bob
  • 4,566
  • 5
  • 42
  • 79

1 Answers1

3

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536