c++/boost use all know that we can easily do reset a smart pointer to a new instance(old one destroy at the same time). I am wondering how do we do that for COM smart pointer ?
_COM_SMARTPTR_TYPEDEF(IMyClass, __uuidof(IMyClass));
//normal class A
class A{
IMyClass m_spIMyClassObj; //this COM smart pointer is a member variable of a normal class
};
I initialize the COM smart pointer with:
m_spIMyClassObj.CreateInstance(__uuidof(MyLib::IMyClass));
This is fine but during the life time of A, I need to reset the COM smart pointer m_spIMyClassObj to a new instance of IMyClass, how do I do that(also making sure the old one id cleaned up).
Thanks
Simply call
m_spIMyClassObj.CreateInstance(__uuidof(MyLib::IMyClass));again.m_spIMyClassObjwill be reassigned to point to the new instance, and the reference count on the old instance will be decreased by one. If there are no outstanding references on the original object, it will destroy itself.