Http://www.cnblogs.com/lanxuezaipiao/p/4132096.html
The STL provides us with four kinds of smart pointers: auto_ptr, Unique_ptr, shared_ptr and weak_ptr
All smart pointer classes have a explicit constructor, with pointers as arguments. For example, Auto_ptr's class template prototype is:
Templet<class t>
class Auto_ptr {
explicit auto_ptr (x* p = 0);
...
};
Therefore, you cannot automatically convert the pointer to a smart pointer object, and you must explicitly call:
shared_ptr<double> PD;
Double *p_reg = new Double;
PD = P_reg; Not allowed (implicit conversion)
PD = shared_ptr<double> (P_reg); Allowed (explicit conversion)
shared_ptr<double> pshared = P_reg; Not allowed (implicit conversion)
shared_ptr<double> pshared (p_reg); Allowed (explicit conversion)
3. Why abandon Auto_ptr. If there is a auto_ptr a pointing to an object, another auto_ptr B; b = A; Then when the a,b expires, a problem arises that A and B will release the heap object is unacceptable because the program will attempt to delete the same object two times.
To avoid this problem, there are several ways to do this: Define the discovered computers value operator so that it performs a deep copy. So two pointers will point to different objects, one of which is a copy of another object, and the disadvantage is wasting space, so the smart pointer does not take this scenario. Establish the concept of ownership (ownership). Only one smart pointer can be owned for a particular object so that only the constructor that owns the object's smart pointer deletes the object. Then let the assignment operation transfer ownership. This is the strategy for auto_ptr and Uniqiie_ptr, but the unique_ptr policy is more stringent. Creates a smarter pointer that tracks the number of smart pointers that reference a particular object. This is called a reference count. For example, when you assign a value, the count is added to 1, and the count is reduced by 1 when the pointer expires. The delete is called when it is reduced to 0 o'clock. This is the strategy adopted by shared_ptr.
<span style= "FONT-SIZE:18PX;" >auto_ptr<string> Pwin;
Pwin = A;
A will lose ownership, transferring ownership of the object to pwin,a into null</span>
The unique_ptr in C++11 is a substitute for auto_ptr, which has the unique property of ownership as Auto_ptr, unlike Auto_ptr, where UNIQUE_PTR does not replicate constructors, which prevents some "quietly" The issue of lost ownership occurred
Compilation error using Unique_ptr, as with Auto_ptr, Unique_ptr takes ownership model, but when using UNIQUE_PTR, the program does not wait until the runtime crashes, and in the compiler because of the following line of code error: Auto_ PTR does not discover errors until run time. In short, when a party program tries to assign a unique_ptr to another, the compiler allows this if the source unique_ptr is a temporary right value, and if the source unique_ptr will exist for some time, the compiler will prohibit this
When using auto_ptr, you need to be aware of the following points: Auto_ptr cannot share ownership; Auto_ptr cannot point to an array; for this, I think it is possible to point to an array if the pod type is stored in the array, or if it contains trivial destructor; Auto_ptr cannot be a member of a container; the C + + standard has expressly prohibited doing so, otherwise there may be unforeseen problems, and if you want to die a bit more miserable, you can do it without a problem.
http://www.jellythink.com/archives/684
http://www.jellythink.com/archives/673