Re: C++ 101 dumb question
"Ben Voigt [C++ MVP]" <rbv@nospam.nospam> wrote in message
news:OZGxPSBtHHA.4916@TK2MSFTNGP04.phx.gbl...
"Anthony Jones" <Ant@yadayadayada.com> wrote in message
news:uxddA$AtHHA.1672@TK2MSFTNGP06.phx.gbl...
"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);
}
Getting close.
Crypto& Crypto::operator = (const Crypto& src)
{
if (this != &src)
{
if (m_hProv) CryptReleaseContext(m_hProv, 0);
m_hProv = src.m_hProv;
if (m_hProv) CryptContextAddRef(m_hProv, 0, 0);
}
}
Thanks dumb logic mistake and & on the src
A couple of issues I'm not sure of:-
Do I need to implement a != operator?
Does comparing Crypto objects to each other make sense? Remember != is
the
opposite of ==, not of =.
I guess I asked that since I wasn't sure how this != src (or this != &src)
was working.
This would be testing the identity of the object as in its address in
memory.
Ultimately I don't know what const Crypto& src is actually doing. I've used
& to make calls to something that uses * but I'm not familiar with & in the
function signature. What's that called?
(C++ is frustrating in that in order to understand such a thing you need to
be able to look it up in the documentation and in order to do that you need
to know what text to look for.)
Can I access the m_hProv private member of src with src.m_hProv?
Yes, any member of Crypto can access private members of any Crypto object,
not just "this".
Wah hey that was a good guess then ;)
Thanks for your patience.