智能指针

1:简列

unique_ptr< double [] > pda(new double(5) );

shared_ptr< int > q;

auto_ptr< char > c;

2:警告

1)使用NEW分配内存时,才能使用auto_ptr shared_ptr,使用new[]分配内存时,不能使用它们。

不使用new分配内存时,不能使用auto_ptr  shared_ptr.不使用new  new[],不能使用unique_ptr.

3:选择智能指针

1)要使用多个指向同一个对象的指针,应选择shared_ptr

2)不需要多个指向同一个对象的指针,可使用unique_ptr

  函数使用new分配内存,并返回指向该内存的指针,则unique_ptr是不错的选择。

原文地址:https://www.cnblogs.com/pengtangtang/p/12781550.html