C++关于智能指针

有四种: auto_ptr<class T> 

               unique_ptr<class T>

               shared_ptr<class T>

               weak_ptr<class T>

这里先讨论前三种:   第一种 :auto_ptr  已摒弃;原因在于:

                                             如 auto_ptr<class T> a(new T);

                                                 auto_ptr<class T> b;

                                                 b=a;         此时a已无所有权,b拥有所有权,再使用a会出错              

                                   第二种:unique_ptr<class T>  在遇到上述时编译器会报错

                                  第三种:shared_ptr<class T>  允许上述,通过引用计数可使多个指针指向同一个

在函数内使用new分配内存时,建议使用智能指针

posted @ 2017-09-02 00:14  Lucky、Dog  阅读(189)  评论(0编辑  收藏  举报