C++ STL智能指针(三)

来源:互联网 发布:c语言搜索引擎 编辑:程序博客网 时间:2024/05/29 13:48
#include <iostream>#include <memory>using std::cout;using std::cin;using std::endl;using std::auto_ptr;class WebSite{public:    WebSite()    {    cout << "WebSite" << endl;    }    ~WebSite()    {    cout << "~WebSite" << endl;    }};int main(){    /* reset()函数的用法    WebSite *pCpp = new WebSite;    auto_ptr<WebSite> pautoWebSite;    pautoWebSite.reset(pCpp);    cout << pCpp << " " << pautoWebSite.get() << endl;    */    auto_ptr<WebSite> autoPtr1(new WebSite);    auto_ptr<WebSite> autoPtr2(new WebSite);    cout << autoPtr1.get() << endl;    cout << autoPtr2.get() << endl;    autoPtr1 = autoPtr2;//发生了三件事:1.autoPtr1析构为0,2.autoPtr2船值给autoPtr1,3.autoPtr2析构为0;    cout << autoPtr1.get() << endl;    cout << autoPtr2.get() << endl;    auto_ptr<WebSite> autoPtr3(autoPtr1); //1.autoPtr1析构为0, 2.将值传递给autoPtr3    cout << autoPtr1.get() << endl; //输出为0    cout << autoPtr3.get() << endl; //输出为autoPtr1之前的值    return 0;}

0 0