C++智能指针auto_ptr使用讲解

auto_ptr是C++98标准库提供的一个智能指针,但已被C++11明确声明不再支持。

auto_ptr具有以下缺陷:

  • auto_ptr有拷贝语义,拷贝后源对象变得无效,这可能引发很严重的问题;而unique_ptr则无拷贝语义,但提供了移动语义,这样的错误不再可能发生,因为很明显必须使用std::move()进行转移。
#include <iostream>
#include <tr1/memory>
using namespace std::tr1;
using namespace std;

class A
{
public:
    string id;
    A(string id):id(id)
    {
    	cout<<id<<":构造函数"<<endl;
    }
    ~A()
    {
    	cout<<id<<":析构函数"<<endl;
    }
};

int main() 
{
    auto_ptr<A> auto_ap(new A("auto_ptr")),auto_bp;
    cout<<auto_ap.get()<<endl;
    auto_bp = auto_ap;
    cout<<auto_ap.get()<<endl;

    unique_ptr<A> unique_ap(new A("unique_ptr")),unique_bp;
    cout<<unique_ap.get()<<endl;
//    unique_bp = unique_ap;  // 报错
    unique_bp = move(unique_ap);
    cout<<unique_ap.get()<<endl;
    return 0;
}


运行结果:

auto_ptr:构造函数
0x6115d0
0
unique_ptr:构造函数
0x6115f0
0
unique_ptr:析构函数
auto_ptr:析构函数
  • auto_ptr不可作为容器元素,unique_ptr可以作为容器元素。因为auto_ptr的拷贝和赋值具有破坏性,不满足容器要求:拷贝或赋值后,两个对象必须具有相同值。
  • auto_ptr不可指向动态数组,unique_ptr可以指向动态数组。因为unique_ptr有unique_ptr<T[]>重载版本,销毁动态对象时调用delete[]。
#include <iostream>
#include <memory>
using namespace std;

class A
{
public:
    string id;
    A(string id):id(id)
    {
    	cout<<id<<":构造函数"<<endl;
    }
    ~A()
    {
    	cout<<id<<":析构函数"<<endl;
    }
};

int main() 
{
    //auto_ptr<A[]> auto_ap(new A[1]{A("unique_ptr")});  // 报错
    unique_ptr<A[]> unique_ap(new A[1]{A("unique_ptr")});
    return 0;
}


  • auto_ptr不可以自定义删除器deleter,而unique_ptr可以。
#include <iostream>
#include <string>
#include <tr1/memory>

using namespace std;
using namespace std::tr1;

class A
{
public:
    string id;
    A(string id):id(id)
    {
    	cout<<id<<":构造函数"<<endl;
    }
    ~A()
    {
    	cout<<id<<":析构函数"<<endl;
    }

};

int main() 
{
    unique_ptr<A,void(*)(A*)> unique_ap(
    new A[2]
    {
		A("unique_ptr0"),A("unique_ptr1")
	},
    [](A *a)
    {
    	delete []a;
    });
    return 0;
}

运行结果:

unique_ptr0:构造函数
unique_ptr1:构造函数
unique_ptr1:析构函数
unique_ptr0:析构函数
#include <iostream>
#include <string>
#include <tr1/memory>
using namespace std;
using namespace std::tr1;

class Test
{
public:
    Test(string s)
    {
        str = s;
       cout<<"Test creat\n";
    }
    ~Test()
    {
        cout<<"Test delete:"<<str<<endl;
    }
    string& getStr()
    {
        return str;
    }
    void setStr(string s)
    {
        str = s;
    }
    void print()
    {
        cout<<str<<endl;
    }
private:
    string str;
};
 
 
int main()
{
    auto_ptr<Test> ptest(new Test("123"));
    ptest->setStr("hello ");
    ptest->print();
    ptest.get()->print();   //成员函数get()返回一个原始的指针
    ptest->getStr() += "world !";
    (*ptest).print();

    //Test creat
    //Test delete:hello world !
    ptest.reset(new Test("123"));  //成员函数reset()重新绑定指向的对象,而原来的对象则会被释放
    ptest->print();

    //ptest2会接管ptest原来的内存管理权,ptest会变为空指针,如果ptest2原来不为空,则它会释放原来的资源
    auto_ptr<Test> ptest2(new Test("456"));
    ptest2 = ptest;
    ptest2.release();
    //ptest2->print(); //段错误 (核心已转储)
    //判断一个智能指针是否为空不能使用if(ptest == NULL),应该使用if(ptest.get() == NULL)
    if(ptest.get() == NULL)
    {
        cout<<"ptest = NULL\n";
    }
    //成员函数是release,这个函数只是把智能指针赋值为空,但是它原来指向的内存并没有被释放,相当于它只是释放了对资源的所有权
    //ptest.release();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42205987/article/details/82947443