Smart pointer of c ++ 11 and smart pointer of 11
This article introduces four smart pointers of c ++, the last three of which are newly added in c ++ 11, and auto _ ptr has been discarded.
To compile c ++ 11, you must install g ++-4.8.
Sudo add-apt-repository ppa: ubuntu-toolchain-r/test
Sudo apt-get update
Sudo apt-get instal gcc-4.8
Sudo apt-get install g ++-4.8
Specify the standard during compilation:
G ++-4.8-std = c ++ 11
Auto_ptrFor details, refer to here
Auto_ptr is the first smart pointer added to the c ++ standard to promote RAII. It implements the most basic resource management, but unlike other types, auto_ptr has no replication semantics. It implements the mobile semantics during replication. When copying and assigning auto_ptr values, the ownership of the managed resources is changed by default. For this reason, it cannot be used in stl containers either, because the elements in the containers must support replicability and assignment. Another factor limiting its use scope is that its destructor calls delete by default and cannot be overloaded. Therefore, it cannot support objects such as arrays.
class A{public: A(int i) :ival(i){ printf("create %d \n",ival); } ~A(){ printf("delete %d \n", ival); } int getvalue(){ return ival;}private: int ival;};auto_ptr<A> ptr1(new A(1));auto_ptr<A> ptr2(new A(2));assert(ptr1->getvalue()==1);assert(ptr2->getvalue()==2);ptr1 = ptr2;assert(ptr2.get ()==NULL); // now ptr2 is NULL
Unique_ptrFor details, refer to here
Due to various problems in auto_ptr, in c ++ 11, unique_ptr is replaced by auto_ptr. They do basically the same thing, but unique_ptr does not support replication semantics and only supports mobile semantics, therefore, you cannot perform common copy or value assignment operations (except when the unique_ptr to be destroyed is returned), but you can assign values to the mobile structure. To put it bluntly, you need to display the move operation when copying. In addition, unique_ptr can be used to reload the deleteer so that it supports arrays and other types.
unique_ptr<A> ptr1(new A(1));unique_ptr<A> ptr2(new A(2));ptr1 = ptr2; //errorptr1 = move(ptr2); //right, ptr2 is NULLassert(ptr2==NULL)unique_ptr<A[]> ptr3(new A[2]{3,4}); //arrayassert(ptr3[0].getvalue()==3);assert(ptr3[1].getvalue()==4);
Shared_ptrFor details, refer to here
Generally, the smart pointer refers to shared_ptr, which allows sharing resources and using reference counting to record resources to be shared by several pointers. When release () is called, the reference count of the previously managed resource is reduced by 1 when the Destructor or the value is copied and assigned, and the resource is released when the count is 0.
shared_ptr<A> ptr1(new A(1));shared_ptr<A> ptr2(new A(2));assert(ptr1->getvalue()==1);assert(ptr1.use_count()==1);assert(ptr2->getvalue()==2);assert( ptr2.use_count())==1);ptr1 = ptr2;assert(ptr1.use_count()==2)assert(ptr2.use_count()==2)assert(ptr1->getvalue()==2);assert(ptr2->getvalue()==2);
Weak_ptrFor details, refer to here
When shared instances are used for mutual reference, resources cannot be released, for example:
class B;class A{public: shared_ptr<B> pb_value; ~A() { cout<<"A delete\n"; } };class B{public: shared_ptr<A> pa_value; ~B() { cout<<"B delete\n"; } };shared_ptr<B> pb(new B());shared_ptr<A> pa(new A());pb->pa_value = pa;pa->pb_value = pb;assert(pa.use_count()==2)assert(pb.use_count()==2)
Reference:
When pa and pb are analyzed, the reference count is equal to 1 and the resource is not released. The solution is to declare any of pa_value and pb_value as weak_ptr.
Week_ptr is a weak reference to an object and does not increase the reference count of the object. Week_ptr and shared_ptr can be converted to each other. shared_ptr can be directly assigned to week_ptr. week_ptr can call the lock function to obtain shared_ptr. If the object has been released, an empty shared_ptr is returned ).
weak_ptr<A> pa_value;
After modification, pb-> pa_value = pa will not increase the Count of pa. pa count is 1. After leaving the scope, the count is 0, and pa is released normally. Therefore, pb count will be reduced to 0, release normally.
Author: coderkian
Source: http://www.cnblogs.com/coderkian/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.