The unique_ptr__c++ of c++11 intelligent pointer

Source: Internet
Author: User
1. Intelligent pointer Concept


A smart pointer is a class (template) that is implemented based on the RAII mechanism, with pointer behavior (overloading the operator* and operator-> operators) to "intelligently" destroy the objects it refers to. C++11 has unique_ptr, shared_ptr and weak_ptr intelligent pointers, can manage dynamic resources 2. Unique_ptr Concept


Unique_ptr "Unique" has its object, and only one unique_ptr at a time can point to a given object (by prohibiting copy semantics, only mobile semantics).

Unique_ptr the life cycle of the pointer itself: starts when the unique_ptr pointer is created, until it leaves the scope. When you leave the scope, if it points to an object, it destroys the object it refers to (by default, the delete operator is used and the user can specify another action).

Unique_ptr the relationship between the pointer and its object: within the life cycle of the smart pointer, you can change the object that the smart pointer refers to, such as when a smart pointer is created by a constructor, by a reset method, by releasing the ownership through the release method, and by transferring the ownership through the move semantics. 3. Basic operation of Unique_ptr:

[CPP] view plain copy//smart pointer creation unique_ptr<int> u_i; Create <span style= "Font-family:arial,helvetica,sans-serif" > "</span><span style=" font-family:Arial, Helvetica,sans-serif "> Null smart pointer" </span> u_i.reset (new int (3)); Binding dynamic Object Unique_ptr<int> u_i2 (new int (4)),//change of ownership when specified dynamic object//on creation int *p_i = U_i2.release ();   Release ownership unique_ptr<string> u_s (new string ("abc")); unique_ptr<string> u_s2 = Std::move (u_s); Transfer of ownership (via mobile semantics), u_s after ownership transfer, becomes a "null pointer" u_s2=nullptr;//explicitly destroys the object, while the smart pointer becomes a null pointer. Use scenario with U_s2.reset () equivalence 4. Unique_ptr


(1) The abnormal security of dynamic resources (using its RAII characteristics):

[CPP] view plain copy void Foo () {//unsafe code x *PX = new X; Do something, exception may occurs delete px; May isn't go here} [CPP] view plain copy void Foo () {//Abnormal safe code.       Regardless of whether an exception occurs, its destructor is invoked as soon as the PX pointer is successfully created, ensuring that the dynamic resource is freed unique_ptr<x> px (new X); Do Something,} (2) returns the dynamic resource created within the function

[CPP] view plain copy unique_ptr<x> foo () {unique_ptr<x> px (new X); Do something return px; The Move Semantics} (3) can be placed in a container (making up for the disadvantage that auto_ptr cannot be a container element)

Mode one:

[CPP] view plain copy vector<unique_ptr<string>> vs {new string{"Doug"}, New string{"Adams"}; Mode two:

[CPP] view plain copy vector<unique_ptr<string>>v;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.