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
1 Odpowiedź
+ 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