C + + auto_ptr

Source: Internet
Author: User

Although auto_ptr is outdated, it still has a lot to do with archaeological work on the antiques.

#include <iostream>#include<memory>using namespacestd;structs{~S () {std::cout<<"~s () called"<<Std::endl; }};voidFoo (std::auto_ptr<s>ptr) {//ptr.release ();}intMain () {std::auto_ptr<S> PTR (NewS);    Foo (PTR); if(!ptr.Get()) {Std::cout<<"ptr is null" ; }} 

The transfer of all rights (always move) is one of the core semantics of auto_ptr. When foo (PTR) is called, the output "PTR is null" will be printed.

To prevent the occurrence of the behavior in Foo, you can set:

1 void Const std::auto_ptr<s>& other ) {2      //    3 }

So what does the copy constructor look like in auto_ptr?

Auto_ptr (const auto_ptr& Other); This is not possible, in order to achieve the sole ownership, we need to other.release (); It is clear that the const has prevented the occurrence of this thing.

Auto_ptr (auto_ptr& other); Right.

So what's the explanation?

Std::auto_ptr<s> makes () {    std::auto_ptr<S> ret ();         return ret;} Std::auto_ptr<S> a = makes ();

C++03 era, makes () returns the right value, the right value can only be passed to Auto_ptr (const auto_ptr& Other), or auto_ptr (auto_ptr&& other); Unfortunately neither of these functions exists.

The solution is: auto_ptr_ref. Essence is a tool for right-grabbing (a pioneering invention in the c++03 ERA). Rvalue std::auto_ptr<s> implicitly converted to Auto_ptr_ref<s>, passed to template< class Y > Auto_ptr (auto_ptr_ref<y> m);

Here is the simulation, the first problem is that c++03 cannot compile:

#include <iostream>using namespacestd;structs{S (inti) {Std::cout<<"S (int i) called \ n"; I_in_s_=i; } s (S&s) {Std::cout<<"s (s) called \ n"; }    intI_in_s_;};intMain () {S B= S (1);}

or the t&. The problem that cannot be bound to the right value is resolved as follows:

#include <iostream>using namespacestd;structs{structs_ref{S_ref (inti): I_ (i) {}intI_;        }; operatorS_ref () {std::cout<<"operator s_ref called \ n"; returnS_ref (I_in_s_); } S (inti) {Std::cout<<"S (int i) called \ n"; I_in_s_=i; } s (S&s) {Std::cout<<"s (s) called \ n"; } S (S_ref r) {std::cout<<"S (s_ref R) called \ n"; I_in_s_=R.i_; }    intI_in_s_;};intMain () {S B= S (1);}

Solve. Thought is: The goal of grasping the right value, nothing more than to copy something out of it. I'll copy the stuff to S_ref and then to S (S_ref R). It also enables the ability to pass data.

The archaeological work was temporarily adjourned.

C + + auto_ptr

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.