On Dec 30, 12:06 am, SeniorLee <prog3...@gmail.comwrote:
in the book EC++ chapter 11, with this code
( pb is a member of class )
WIdget& WIdget::operator=(const WIdget& rhs)
{
Bitmap *pOrig = pb;
pb = new Bitmap(*rhs.pb);
delete pOrig;
return *this;
}
book says this code is safe for exception, but what if exception
occurs in " pb = new Bitmap(*rhs.pb);" this line?
and it's going to run the next line(delete pOrig;). this will result
this object lose its bitmap pointer.
Your arguement is mute, if that new allocation throws, everything
after new is skipped, including the return.
Furthermore, the program will unwind each calling scope(s)'s stacks
progressively until the exception is caught ( or in the event thrown
exceptions are left uncaught: terminate() gets called ).
>
this is right? or wrong?
Its right, assuming that member pb was initialized appropriately
beforehand.
Also, that assignment operator should be checking for self-assignment.
WIdget& WIdget::operator=(const WIdget& rhs)
{
if (&rhs == this)
return *this;
// do assignment stuff
return *this;
}