I am working on a catch all smart pointer class that I can throw in my header based library for easy library-less implementation.
I can't seem to find much examples online on how to code such a class. The concept is fairly simple so I have an idea how to do it already.
Does such class already exist somewhere where I can literally just do a copy paste, or can someone point me in the right direction to ensure I'm on track? I don't want library based classes like boost, I want something very small and portable, and if I code it myself I'll also have a better understanding of how it works.
Basically from my understanding the concept is that when an instance is created it also creates a real pointer holding the real data. That pointer also has a variable to keep track of "count". Starts at 0. Every time an instance of the smart pointer is copied from the original, it refers to the existing pointer and increments the count. When it's dereferenced (destructor called) it deducts the count. If the count is 0, it deletes the pointer. Is this pretty much how they work? I coded something similar to this in my socket server class and it seems to have proven bullet proof in my valgrind testing. Now to make a template based class so I can use it everywhere. I'm writing a large scale app and have been using raw pointers and before the app gets too big, think I will try to clean those up and convert to smart pointers.
I can't seem to find much examples online on how to code such a class. The concept is fairly simple so I have an idea how to do it already.
Does such class already exist somewhere where I can literally just do a copy paste, or can someone point me in the right direction to ensure I'm on track? I don't want library based classes like boost, I want something very small and portable, and if I code it myself I'll also have a better understanding of how it works.
Basically from my understanding the concept is that when an instance is created it also creates a real pointer holding the real data. That pointer also has a variable to keep track of "count". Starts at 0. Every time an instance of the smart pointer is copied from the original, it refers to the existing pointer and increments the count. When it's dereferenced (destructor called) it deducts the count. If the count is 0, it deletes the pointer. Is this pretty much how they work? I coded something similar to this in my socket server class and it seems to have proven bullet proof in my valgrind testing. Now to make a template based class so I can use it everywhere. I'm writing a large scale app and have been using raw pointers and before the app gets too big, think I will try to clean those up and convert to smart pointers.