+ 1

Shared pointer use count after reset

Refer code below: https://www.sololearn.com/compiler-playground/c8i3yyr0wNWx You should mainly focus on last few lines of main function. Initially, spIT2.use_count() results into 2 (one is object itself and other count is added due to AddReportee). This 2 seems correct to me. Once I do spIT2.reset(); , doesn't it mean it reduces only 1 reference? Why subsequent use_count shows 0 instead of 1? https://code.sololearn.com/c8i3yyr0wNWx/?ref=app

29th Aug 2023, 9:09 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Respuestas
+ 1
after spIT2.reset();, spIT2 is empty (i.e. does not point to any object). according to the standard: " "std::share_ptr::use_count" returns 0 when *this is empty. "
30th Aug 2023, 1:55 PM
MO ELomari
MO ELomari - avatar
+ 2
Thanks for this. It helped me understand it in bit more details. I have another question posted as below: https://www.sololearn.com/Discuss/3236988/query-on-use_count-of-shared-pointer Would be great if you can share your thoughts. By the way, many thanks again for your help.
3rd Sep 2023, 7:55 AM
Ketan Lalcheta
Ketan Lalcheta - avatar