Home › Forums › C Programming › Smart pointers…
- This topic has 3 replies, 4 voices, and was last updated 18 years, 8 months ago by msaqib.
- AuthorPosts
- August 19, 2004 at 11:23 am #1864ARJJUNTILAKParticipant
Hello, its my first itme here,
can anyone tell me what are the smart pointers in c/c++ and what is their use.
Thanks
Joney - October 8, 2004 at 1:08 pm #3111willParticipant
Smart pointers are C++ objects that simulate simple pointers by implementing operator-> and the unary operator*. In addition to sporting pointer syntax and semantics, smart pointers often perform useful tasks—such as memory management or locking—under the covers, thus freeing the application from carefully managing the lifetime of pointed-to objects.
The simplest example of a smart pointer is auto_ptr, which is included in the standard C++ library. You can find it in the header
, or take a look at Scott Meyers’ auto_ptr implementation. Here is part of auto_ptr’s implementation, to illustrate what it does: 1234567891011template <class T> class auto_ptr<br />{<br />T* ptr;<br />public:<br />explicit auto_ptr(T* p = 0) : ptr(p) {}<br />~auto_ptr() & nbsp; & nbsp; {delete ptr;}<br />T& operator*() & nbsp; {return *ptr;}<br />T* operator->() &nb sp; {return ptr;}<br />// ...<br />};<br /></class>You can find many readings on smart pointers. I have found two very interesting readings on smart pointers.
http://ootips.org/yonat/4dev/smart-pointers.html
http://www.informit.com/articles/article.asp?p=31529 - February 11, 2005 at 1:06 pm #3112prasannaParticipant
To generalize the type universe of smart pointers, we distinguish three potentially distinct types in a smart pointer:
The storage type. This is the type of pointee_. By “default”—in regular smart pointers—it is a raw pointer.
The pointer type. This is the type returned by operator->. It can be different from the storage type if you want to return a proxy object instead of just a pointer. (You will find an example of using proxy objects later in this chapter.)
The reference type. This is the type returned by operator*.
It would be useful if SmartPtr supported this generalization in a flexible way. Thus, the three types mentioned here ought to be abstracted in a policy called Storage.
In conclusion, smart pointers can, and should, generalize their pointee type. To do this, SmartPtr abstracts three types in a Storage policy: the stored type, the pointer type, and the reference type. Not all types necessarily make sense for a given SmartPtr instantiation. Therefore, in rare cases (handles), a policy might disable access to operator-> or operator* or both.
- April 11, 2006 at 6:54 am #3113msaqibParticipant
In computer science, a smart pointer is an abstract data type that simulates a pointer while providing additional features, such as automatic garbage collection or bounds checking. These additional features are intended to reduce bugs caused by the use of pointers while retaining efficiency. Smart pointers typically keep track of the objects they point to for the purpose of memory management.
For comlete reading goto: http://en.wikipedia.org/wiki/Smart_pointer
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.