smart pointer

A smart pointer is an abstract data type that stimulates a pointer with additional funationality, such as auto memory deallocation, reference counting, etc. In practise it could be a wrapper around of a regular pointer and forwards all meaningfull operations to the pointer

The simplest smart pointer is autr_ptr which are defined in header file <memory>

 

复制代码
template <class T> auto_ptr {

public:
    explicit auto_ptr(T* p): auto_ptr(p){}
     ~auto_prt() {delete ptr;}
    T& operator*(){return *ptr;}
    T* operator->(){return &ptr;}
private: 
    T*  ptr;
    // other functionalities...
};
复制代码

 

 

 

 

reference:

- http://en.wikipedia.org/wiki/Smart_pointer

- http://ootips.org/yonat/4dev/smart-pointers.html

posted @   LevyFan  阅读(719)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示