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);
*pointer = 14;
return 0;
}
It is very simple. I hope my article helped you.
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)