c++ - assign new shared_pt object to a[1] , the reference count of the object original contained in a[1] decrease. How? -
In the code below, a 1 [1] point has been assigned a new value p3 on first 1p. The reference of the item indicated by P1 decreased to the number 1, which is the correct behavior. But I can not understand how this can be implemented. Is it done by the compiler?
int main () {{cout & lt; & Lt; "1" & lt; & Lt; Endl; Std :: tr1 :: share_ptr & lt; C & gt; P0 (new c (0)); Std :: tr1 :: shared_ptr & lt; C & gt; P1 (new C (1)); Std :: tr1 :: shared_ptr & lt; C & gt; A [2]; A [0] = p0; A [1] = p1; {Cout & lt; & Lt; "P1 Count" & lt; & Lt; P1.use_count () & lt; & Lt; Endl; // print2std :: tr1 :: share_ptr & lt; C & gt; P3 (New C (3)); // p1 reference count has decreased how it is affected? A [1] = p3; Cout & lt; & Lt; "P1 Count" & lt; & Lt; P1.use_count () & lt; & Lt; Endl; // print1}}}
expression a [1] = p 3 Assignments of shared APTR inviting operator This assignment operator must first subtract the reference counter of the referenced object and increase the reference counter of the newly referenced object. The reference counter is usually stored with reference objects, not the pointer with the referenced smart.
Comments
Post a Comment