Mahesh
Mahesh

Reputation: 34625

CComPtr pass by reference

I have a situation where I need to pass a CComPtr<IXmlReader> to a function by reference. Does the called parameter takes the ownership from the callee parameter (or) the reference count is increased?

void foo( CComPtr<IXmlReader> & pReader )
{
  // There is no reassignment of the CComPtr.
  // Just call the IXmlReader methods.
}

CComPtr<IXmlReader> pReader;
foo( pReader );

// Is pReader still valid after the function return ?

Thanks.

Upvotes: 0

Views: 3307

Answers (2)

Roman Ryltsov
Roman Ryltsov

Reputation: 69706

No reference counting affected by the call itself, it is only inner function manipulation with the pointer which might be adding or releasing number of references to the object.

// Is pReader still valid after the function return ?

Yes, unless the function re-assigned the value, setting it to NULL or putting in there a new value.

Even if the argument in input-only, there is a programmer's bonus in passing arguments this way: (1) you are completely staying within domain of smart pointers and you don't need to worry a lot about proper reference counting (2) you have assertion failure on e.g. trying to -> an uninitialized pointer (3) release build optimizations will/might be generating nearly as fast code as if you used raw pointer instead.

Upvotes: 0

dsharlet
dsharlet

Reputation: 1066

If there is no reassignment, why is it a reference parameter?

Regardless, there is no change in the reference count. Yes, the CComPtr is still valid after returning.

Upvotes: 1

Related Questions