Re: C++ 101 dumb question
"Ben Voigt [C++ MVP]" <rbv@nospam.nospam> wrote in message
news:%234mbjmAtHHA.1672@TK2MSFTNGP06.phx.gbl...
Define operator= to close the handle previously held in the object, and
use
DuplicateHandle to get a copy of the right-hand-side's handle. But be
careful about x = x, when (this == &rhs) you should just do nothing.
Thanks Ben. Now that I'm beginning to under the copy constructor and
assigment operator a little better. The question of dealing with members
which specifically are handles did cross my mind.
In a copy constructor I merely have to duplicate the handle in the src.
In an assignment I need to release any existing handle and duplicate the one
from the source. A special case arises where src and this are the same
object. In this case do nothing otherwise I'll be attempting to duplicate a
handle I've just released.
The specific case for Cryptographic contexts duplicate handle doesn't apply.
CryptContextAddRef is used instead to increase the number of
CryptoReleaseContexts needed to actually release the context.
Crypto& Crypto::operator = (const Crypto& src)
{
if (this != src)
{
if (m_hProv) CryptReleaseContext(m_hProv, 0);
}
m_hProv = src.m_hProv;
CryptContextAddRef(m_hProv, 0, 0);
}
A couple of issues I'm not sure of:-
Do I need to implement a != operator?
Can I access the m_hProv private member of src with src.m_hProv?