Here is an example snippet:
_variant_t var;
var.vt = VT_UNKNOWN;
var.punkVal = unknownInterfaceSmartPointer;
unknownInterfaceSmartPointer->AddRef(); // Question Statement
// Setting unknownInterfaceSmartPointer to some other container
Whether manual AddRef is really needed here? Following will is better way than this:
_variant_t var(unknownInterfaceSmartPointer.GetInterfacePointer());
your thoughts?
If you directly assign to
punkValmember (as in your first snippet) then you need anAddRef()– noone will do it for you. A much better way would be to useoperator=()member of_variant_t:or do as you suggest – use a conversion constructor:
Both latter variants are good and don’t require an
AddRef()in your code – it will be done inside_variant_timplementation. Furthermore, they are beneficial because if you reassign to the same variant the implementation will do proper cleanup for you: