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引用计数智能指针:可以和其他 boost::shared_ptr 类型的智能指针共享所有权。 在这种情况下,当引用对象的最后一个智能指针销毁后或者被重新赋值或者使用了reset(),对象才会被释放。多个shared_ptr对象可以拥有同一个对象。
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.
仅仅观察被管理的对象,对其生命周期不产生任何影响
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/
否则会出现的错误 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类有了一个weak_ptr 作为成员变量。当第一个shared_ptr创建时,从第一个shared_ptr中初始化该weak_ptr/,当需要一个指向this的share_ptr时调用shared_from_this()成员函数,返回一个由weak_prt/构造而来的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构造函数,和copy assignment操作符,不允许一个对象同时被多个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构造函数和move assignment 操作符使得unique_ptr的所属权从原来的转移到新的。转移之后原来的unique_ptr不包含任何对象。
隐式的从右值转换
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进行转换
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
新闻热点
疑难解答
图片精选