Boost logo

Boost Users :

From: Philipp Henkel (threadpool_at_[hidden])
Date: 2007-01-04 15:36:11


> > CComPtr-like usage:
> > boost::intrusive_ptr<IVideo> video;
> > someObject->QueryInterface(IID_IVideo,(void**)&video);
> >
> > Is there a better way? How do you bring intrusive_ptr and
> > QueryInterface together?
>
> The technically correct way to use QueryInterface is to pass it the
> address
> of a void*:
>
> void* pv = 0;
> someObject->QueryInterface( IID_IVideo, &pv );
> IVideo * pv2 = static_cast< IVideo* >( pv );
> boost::intrusive_ptr<IVideo> video( pv2 );
> pv2->Release();

Thanks for highlighting the ideal QueryInterface usage.

The (void**) cast is too error-prone, even if you don't care about
> technicalities.

Microsoft's CComPtr pushed me in that direction. Their CComPtr provides an
operator&, too.

This of course can be encapsulated in a function.
>
> The encapsulation seems to be perfect in most cases. Unfortunately I've to
cope with some legacy (template) code for CComPtrs which relies on
operator&. :-/ That's why I tried to provide it for intrusive_ptr. Is there
a problem beside the void cast with my operator or its implementation?
Of course, I can change the operator's signature to avoid the cast:
template<typename T> void** operator&(intrusive_ptr<T>& ptr)
By doing this nobody uses the operator by accident (or without thinking
about it).

Please note that I intend to use the operator with my legacy code only. In
all other cases I'll prefer your query interface template.

Thanks,
Philipp



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net