How to manage count of own shared pointer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to manage count of own shared pointer

Hi Tried to implement shared pointer as below: What should be done to increase count of existing variable as I needed to pass obj in copy constructor as const to allow copy from const obj Even if I don't use const in the copy constructor and increment the value , still count is not getting released Any suggestions plz https://code.sololearn.com/c6V7q43m8zXA/?ref=app

2nd May 2022, 3:51 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Answer
+ 2
A local counter will not work as the destructor of one shared pointer is unable to decrement the local counters of other owning instances. As a consequence, the pointer can never be deleted once a copy has been made. One possible way around this is to allocate the reference counter on the heap, thus sharing it between owning instances (as well as possible weak pointers). For further implementation ideas, I would suggest you to search the web, for example: https://stackoverflow.com/questions/9200664/how-is-the-stdtr1shared-ptr-implemented
2nd May 2022, 9:12 PM
Shadow
Shadow - avatar