#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; }
C++ STL智能指针(三)
最新推荐文章于 2024-08-30 19:53:45 发布
