Deal with c++11’s smart pointer facility.
Smart pointers are class objects that behave like built-in pointers but also manage objects that you create with new so that you don’t have to worry about when and whether to delete them - the smart pointers automatically delete the managed object for you at the apPRopriate time.
shared_ptrweak_ptrunique_ptr引用計(jì)數(shù)智能指針:可以和其他 boost::shared_ptr 類型的智能指針共享所有權(quán)。 在這種情況下,當(dāng)引用對(duì)象的最后一個(gè)智能指針銷毀后或者被重新賦值或者使用了reset(),對(duì)象才會(huì)被釋放。多個(gè)shared_ptr對(duì)象可以擁有同一個(gè)對(duì)象。
There are actually two dynamic memory allocations that happen: one for the object itself from the new, and then a second for the manager object created by the shared_ptr constructor. Since memory allocations are slow, this means that creating a shared_ptr is slow relative to using either a raw pointer, or a so-called “intrusive” reference- counted smart pointer where the reference count is a member variable of the object. To address this problem, C++11 includes a function template make_shared that does a single memory allocation big enough to hold both the manager object and the new object, passing along any constructor parameters that you specify, and returns a shared_ptr of the specified type, which can then be used to initialize the shared_ptr that you are creating (with efficient move semantics).
shared_ptr<Thing> p(new Thing); // ouch - two allocations shared_ptr<Thing> p1(make_shared<Thing>()); // only one allocation!Weak pointers just “observe” the managed object; they don’t “keep it alive” or affect its lifetime. Unlike shared_ptrs, when the last weak_ptr goes out of scope or disappears, the pointed-to object can still exist because the weak_ptrs do not affect the lifetime of the object - they have no ownership rights. But the weak_ptr can be used to determine whether the object exists, and to provide a shared_ptr that can be used to refer to it.
僅僅觀察被管理的對(duì)象,對(duì)其生命周期不產(chǎn)生任何影響
1.weak_ptr build-in-pointer might zero.
void do_it(weak_ptr<Thing> wp){shared_ptr<Thing> sp = wp.lock();// get shared_ptr from weak_ptr if(sp) sp->defrangulate(); // tell the Thing to do something else cout << "The Thing is gone!" << endl;}2.This approach is useful as a way to simply ask whether the pointed-to object still exists.
bool is_it_there(weak_ptr<Thing> wp) { if(wp.expired()) { cout << "The Thing is gone!" << endl; return false; } return true; }3.if the weak_ptr is expired, an exception is thrown, of type std::bad_weak_ptr.
void do_it(weak_ptr<Thing> wp){shared_ptr<Thing> sp(wp); // construct shared_ptr from weak_ptr // exception thrown if wp is expired, so if here, sp is good to go sp->defrangulate(); // tell the Thing to do something} try { do_it(wpx); } catch(bad_weak_ptr&) { cout << "A Thing (or something else) has disappeared!" << endl; }4.inherit from enabled_shared_from_this/
否則會(huì)出現(xiàn)的錯(cuò)誤 error:pointer being freed was not allocated
class Thing:public enable_shared_from_this<Thing>{public: int id;public: virtual void showID() const; Thing(); Thing(int _id); void foo();};void Thing::foo() { shared_ptr<Thing> t1 = shared_from_this(); t1->showID();}公有繼承enable_shared_from_this/ ,則Thing類有了一個(gè)weak_ptr 作為成員變量。當(dāng)?shù)谝粋€(gè)shared_ptr創(chuàng)建時(shí),從第一個(gè)shared_ptr中初始化該weak_ptr/,當(dāng)需要一個(gè)指向this的share_ptr時(shí)調(diào)用shared_from_this()成員函數(shù),返回一個(gè)由weak_prt/構(gòu)造而來的shared_ptr/,使得返回的shared_ptr與第一次的shared_ptr是相同的 manage object.
With a unique_ptr, you can point to an allocated object, and when the unique_ptr goes out of scope, the pointed-to object gets deleted, and this happens regardless of how we leave the function, either by a return or an exception being thrown somewhere.
unique_ptr implements a unique ownership concept - an object can be owned by only one unique_ptr at a time - the opposite of shared ownership.
unique_ptr 隱式的刪除了copy構(gòu)造函數(shù),和copy assignment操作符,不允許一個(gè)對(duì)象同時(shí)被多個(gè)unique_ptr擁有這恰恰與shared_ptr相反。
The unique ownership is enforced by disallowing (with =delete) copy construction and copy assignment.So unlike built-in pointers or shared_ptr, you can’t copy or assign a unique_ptr to another unique_ptr.
move semantics: the move constructor and move assignment Operator are defined for unique_ptr so that they transfer ownership from the original owner to the new owner.
可以通過move構(gòu)造函數(shù)和move assignment 操作符使得unique_ptr的所屬權(quán)從原來的轉(zhuǎn)移到新的。轉(zhuǎn)移之后原來的unique_ptr不包含任何對(duì)象。
隱式的從右值轉(zhuǎn)換
unique_ptr<Thing> create_Thing() { unique_ptr<Thing> local_ptr(new Thing); return local_ptr; // local_ptr will surrender ownership }void foo() { unique_ptr<Thing> p1(create_Thing()); // move ctor from returned rvalue // p1 now owns the Thing unique_ptr<Thing> p2; // default ctor'd; owns nothing p2 = create_Thing(); // move assignment from returned rvalue // p2 now owns the second Thing}顯式的使用move assignment 和 move construction進(jìn)行轉(zhuǎn)換
unique_ptr<Thing> p1(new Thing); // p1 owns the Thingunique_ptr<Thing> p2; // p2 owns nothing// invoke move assignment explicitlyp2 = std::move(p1); // now p2 owns it, p1 owns nothing// invoke move construction explicitlyunique_ptr<Thing> p3(std::move(p2)); // now p3 owns it, p2 and p1 own nothingc++11 smart pointer
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注