Tuesday, July 14, 2009

What is meant by Smart Pointer in C++ Language?

Where it necessarily in use and advantages to use this pointer?


If possible give me small snippet

What is meant by Smart Pointer in C++ Language?
In C++ you can create objects that act like pointers, but are "smarter" (well depends on how they are implemented..). Theoretically speaking they are called smart pointers simply because it usually does more actions then a default pointer.


Of course when creating these kind of objects you must design them as to have the same interface as normal pointers, meaning support pointer operations like dereferencing, indirection..


In some books you won't find the term of smart pointers, but you'll find the term of design patterns and even Pattern Oriented Arhitecture. A pattern is an object that looks and feels like something else.


In C++ standard library, there are already included some "smart" pointers, and one of them is auto ptr for example. auto ptr is generally used when you don't want to worry about cleaning up the memory after using normal pointer (auto ptr cleans up after itself automatically).


You can even create a pointer that reacts differently when being deleted. (so called pointer to NULL...)


No comments:

Post a Comment