Coding a smart pointer class

Red Squirrel

No Lifer
May 24, 2003
68,461
12,613
126
www.anyf.ca
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.
 

pdusen

Member
May 8, 2008
39
0
0
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.

Boost.SmartPtr is a header-only library. All you have to do is include the header. Of course, it includes several other boost headers, so if you're specifically looking to only include a single header, then that would be a problem.

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.

That's pretty much right for your basic smart pointer. There are other issues to consider, however, depending on how generic you want your implementation to be (for example, there are strategies for writing working smart pointers for a multithreaded environment).

For a really good run-down of the issues and techniques for writing a smart pointer, you might look up the book Modern C++ Design. Chapter 7 is basically a lengthy tutorial on the design decisions and implementation of a Smart Pointer class, specifically the one in the Loki library.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
Really, this shouldn't be too hard, you need a destructor, a constructor, and to overload =, and possibly + and -

In the constructor, set the real pointer to null. In the destructor, check if it is not null, if so, delete it. and = + and - will be for ease of use. (I guess you might want [] as well)
 

pdusen

Member
May 8, 2008
39
0
0
Really, this shouldn't be too hard, you need a destructor, a constructor, and to overload =, and possibly + and -

In the constructor, set the real pointer to null. In the destructor, check if it is not null, if so, delete it. and = + and - will be for ease of use. (I guess you might want [] as well)

That example doesn't allow for multiple pointers to the same data. But the OP already basically understands how to do that.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |