About user operator = () from Meyers book

I am reading S. Meyers' book Effective C ++. 55 Concrete Ways ... (3rd edition). And something in this book, in rule 11, I don’t understand. So in the next part of the code:

Widget& Widget::operator=(const Widget& rhs)
{
    Bitmap *pOrig = pb;
    pb = new Bitmap(*rhs.pb)
    delete pOrig;
    return *this;
}

Why is pOrig used?

PS Sorry for my bad english.

+4
source share
1 answer

Since you are dealing with raw pointers here, you must do the proper resource management. In this case, there is a possibility that it new Bitmap(...)can quit, for example, if the process runs out of memory, and it does not change the value of what pb indicates, since an exception will be thrown before the assignment is made.

, Bitmap , new , , .

, pb, new Bitmap... , .

, , , , .

+12

All Articles