Click here to Skip to main content
15,867,964 members
Articles / Programming Languages / C++
Article

Simple Smart Pointer Implemantation

Rate me:
Please Sign up or sign in to vote.
2.59/5 (18 votes)
13 Sep 2006 27.2K   13   4
Implementaion of a simple smart pointer.

Introduction

Sometimes it is necessary to use something automatic instead of classical C++ pointers. For example you want that your pointer is automatically deleted. For this we need a class that automatize this things. Such class is called Smart Pointer.

template<class T><BR>class SmartPointer<BR>{<BR>public:<BR> SmartPointer(T* pointer)<BR>  : mPointer(pointer)<BR> {}<BR> ~SmartPointer()<BR> {<BR>  delete mPointer;<BR>  release();<BR> }<BR> T* get()<BR> {<BR>  return mPointer;<BR> }<BR> void release()<BR> {<BR>  mPointer = NULL;<BR> }<BR> bool valid() const<BR> {<BR>  return mPointer != NULL;<BR> }<BR> T* operator-> ()<BR> {<BR>  return mPointer;<BR> }<BR> T& operator* ()<BR> {<BR>  return *mPointer;<BR> }<BR>private:<BR> T* mPointer;<BR> SmartPointer(SmartPointer&) {}<BR> SmartPointer& operator= (SmartPointer&) {}<BR>};

We need a template class, because it should work with each type. The function get() returns the pointer, with the function valid() you can check whether the pointer has a valid value or not and the function release() sets the value of the pointer to NULL. We overload two operators: -> for the pointer and * for the value of the pointer. The most important thing of the smart pointer is this:

~SmartPointer()
{
  delete mPointer;
  release();
}

In the desctructor we automatically delete the pointer and then we set the value of the pointer to NULL.

This is an example how to use the smart pointer:

int main(int, char**)
{
 SmartPointer<int> pointer(new int); /* create a smart pointer with the type int */
 *pointer = 14; /* set the value to 14 */
 /* ... */
 return 0; 
} /* at the end of the scope the pointer will be automatically deletet */

It is very simple. I hope my article helped you.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Austria Austria
When I was younger I looked on my father when he programmed something. First it was not interesting for me. Later I asked my father to teach me programming. He began to teach me BASIC. Time later I told him it is not so interesting, because that programming language looked very old as that he programmed. 6 months later I programmed some simple applications in Visual Basic. Then there was a month break between me and programming. Then I found more interesting programming language for me, PASCAL. I tried it 3 weeks then I found a book about C#. I began to read it and it was very interesting and something new for me. My father taught me the fundamentals of the programming language and then I programmed some simple console applications, later text editor with syntax coloring. I programmed C# for 3 months. Later I wanted to know how does it work in the background. I started to programme in C++. My father taught me the very basic things in C++, but there were very things that were similar to C# then I borrowed me some books from the library about C++. When I was better in C++, I made some exercises about algorithms and datastructures in C++ with my father. After, I decided me to make my own project. I began to programme at my shell. I used WIN32-API in my project. I like many things in C++, for example that I can programme procedural and object-oriented and lot of other things. I thing C++ is for the people that want to know how does it work in the background. C# is for object-oriented programming beginners and for the Windows GUI programmers. I like programming and in the future I plan to programme C++. Maybe I make some new project.
(special thanks for my father AzazelDev)

Comments and Discussions

 
QuestionWhat about reference counting ? Pin
cristitomi14-Mar-07 23:39
cristitomi14-Mar-07 23:39 
GeneralLuhu Pin
Analpopowichser18-Sep-06 7:53
Analpopowichser18-Sep-06 7:53 
GeneralMake it non-copyable Pin
Nemanja Trifunovic13-Sep-06 9:04
Nemanja Trifunovic13-Sep-06 9:04 
GeneralSimple but elegant Pin
cybernaut_ev13-Sep-06 8:51
cybernaut_ev13-Sep-06 8:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.