mercredi 22 juillet 2015

C++ smart pointer counter

Blob<string> b1; //empty Blob
{ //new scope
    Blob<string> b2 = {"a","b","the"};
    b1 = b2;//b1 and b2 share the same elements
}//b2 is destroyed, but the elements in b2 must not be destroyed
 //b1 points to the elements originally created in b2

My question is: why shouldn't b1 on line 4 be destroyed? In my opinion, assigning one shared_pointer to another increments the count of the right-hand operand and decrements the count in the left-hand operand. I think on line4, b1 goes to 0(counter), so it should be destroyed.

Aucun commentaire:

Enregistrer un commentaire