So we have this code
_com_ptr_t& operator=(_com_ptr_t&& cp) throw()
{
if (m_pInterface != cp.m_pInterface) {
Interface* pOldInterface = m_pInterface;
m_pInterface = cp.m_pInterface;
cp.m_pInterface = nullptr;
if (pOldInterface != nullptr) {
pOldInterface->Release();
}
}
return *this;
}
The pOldInterface
is Release()d
on move assignment. Why are move assignment/constructor operations not implemented as swaps which lets the Release()
occur naturally on moved object's destruct or just use the nullptr
assignment or Release()
to manually trigger it early?
I always implement move constructors as swap operations. Is this bad practice?
My code would be
_com_ptr_t& operator=(_com_ptr_t&& cp) throw()
{
if (m_pInterface != cp.m_pInterface) {
Interface* pOldInterface = m_pInterface;
m_pInterface = cp.m_pInterface;
cp.m_pInterface = pOldInterface;
// or just std::swap(m_pInterface, cp.m_pInterface);
}
return *this;
}
Is there a reasoning behind MS _com_ptr_t
choice? This question also applies to any move assignment/constructor so this context is more/less relevant. It's all about whether we release data or we swap it?
Aucun commentaire:
Enregistrer un commentaire