Tuesday, March 15, 2011

Is it best practice to use COM properties or COM setters and getters in C++?

I am relatively new to development within COM, and I was wondering what the community standard was for access of COM object properties. I have seen both of the following conventions in code:

comObjectPtr->PutValue(value);

and

comObjectPtr->Value = value;

and both seem to work, but I was wondering if there was an inherent advantage or disadvantage to either approach and why.

From stackoverflow
  • You're talking about the smart wrapper classes created with #import, right?

    PutValue() is more explicit as to what you're doing; "Value = " is easier to use but it can have "hidden" side-effects if the put function does something other than a straight assignment. As I understand it, the two are equivalent, just different syntaxes.

    Oliver N. : This is what I'm talking about.
    Jason S : As much as I appreciate getting this answer accepted, @crashmstr's answer points out a more important subtlety, namely the exception vs. HRESULT return value.
  • If I remember correctly, using the property assignment will throw an exception vs. a HRESULT returned in the setter if there is a problem.

    Same thing is true of the getter method calls vs. property.

    A "benefit" of using the property is that you can use the prop-get values directly instead of having to declare a variable and getting it before using.

    Jason S : Good point re: the HRESULT vs. exception.

0 comments:

Post a Comment