The 27th question of C/C + + Smart pointer for school recruit interview (UNIQUE_PTR)

Source: Internet
Author: User

auto_ptr<string> P1 (New string ("Auto");//#1auto_ptr <string> p2;                       #2p2 = p1;                                   #3

In the statement # #, P2 takes over ownership of the string object, and the ownership of the P1 is stripped. As I said earlier, this is a good thing to prevent P1 and P2 destructors from trying to delete the same object;

But if the program then tries to use P1, it would be a bad thing, because P1 no longer points to valid data.

Let's look at the use of unique_ptr:

Unique_ptr<string> P3 (New string ("Auto");   #4unique_ptr <string> P4;                       //#5p4 = P3;  #6                   

The compiler considers the statement to be illegal, avoiding the problem that P3 no longer points to valid data. Therefore, UNIQUE_PTR is more secure than auto_ptr.

But there's a smarter place for unique_ptr.
Sometimes a smart pointer is assigned to another hanging pointer that does not leave a danger. The following function definitions are assumed:

Unique_ptr<string> Demo (const char * s) {    unique_ptr<string> temp (new string (s));     return temp;}

And suppose you write the following code:

Unique_ptr<string> Ps;ps = Demo (' uniquely special ');

The demo () returns a temporary UNIQUE_PTR, and then PS takes over all the objects that were originally returned to the unique_ptr, and the temporary unique_ptr is destroyed when it returns, that is, there is no chance to use UNIQUE_PTR to access the invalid data, in other words, This assignment does not cause any problems, that is, there is no reason to prohibit such assignment. In fact, the compiler does allow this assignment, which is where unique_ptr is smarter.

in summary, when the program attempts to assign a unique_ptr to another, if the source unique_ptr is a temporary rvalue, the compiler allows this, and if the source unique_ptr will exist for some time, the compiler will disallow this , such as:

Unique_ptr<string> pu1 (New string ("Hello World"));unique_ptr<string> pu2;pu2 = pu1;                                      #1 not allowedunique_ptr<string> pu3;pu3 = unique_ptr<string> (The new string ("You"));   #2 allowed

#1留下悬挂的unique_ptr (PU1), which can cause harm. and # # does not leave the dangling unique_ptr because it calls the Unique_ptr constructor, which creates a temporary object that is destroyed after its ownership is given to PU3. This behavior, as the case may be, suggests that unique_ptr is better than allowing two auto_ptr to be assigned.

Of course, you might really want to do something like # #, which is unsafe only when you use a discarded smart pointer in a non-intelligent way, such as when you dereference it. To safely reuse this pointer, you can assign a new value to it. C + + has a standard library function, Std::move (), that allows you to assign one unique_ptr to another. Here is an example of using the demo () function, which returns a Unique_ptr<string> object:
With move, the original pointer still transfers ownership into a null pointer , which can be re-assigned.

Unique_ptr<string> PS1, Ps2;ps1 = Demo ("Hello");p s2 = Move (PS1);p S1 = Demo ("Alexia"); cout << *ps2 << *ps 1 << Endl;

  

The 27th question of C/C + + Smart pointer for school recruit interview (UNIQUE_PTR)

Related Article

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.