Boost Library Shared_from_this

Source: Internet
Author: User

Working with stories:

When a class object is managed by shared_ptr, it is necessary to pass the current class object as an argument to the other function in a function defined by the class, at which point a shared_ptr must be passed, otherwise the semantics of the class object cannot be maintained shared_ptr (because there is a raw Pointer points to this class object, and shared_ptr the reference to the class object is not counted, it is likely that shared_ptr has freed the class object resource, and that calling function is still using the class object-Obviously, this will certainly produce an error.
How to use:
For a Class A, when we want to use shared_ptr to manage its class objects, and we need to shared_ptr the class object in the function we define (why not the normal pointer, when we use the smart pointer to manage the resource, we must use the smart pointer uniformly, And you can't use a smart pointer in some places using raw pointer, otherwise you can't keep the semantics of the smart pointer, resulting in a variety of errors when passing to other functions, you can let class A inherit from Enable_shared_from_this:
Class A:public Boost::enable_shared_from_this<a> {
};
The Shared_from_this function is then used in class A to pass the shared_ptr of the class object itself to obtain shared_ptr pointing to itself.

(Shared_from_this () uses the Shared_from_this function in a class where it is necessary to pass the shared_ptr of the class object itself to obtain the shared_ptr that points to itself, which is enable_shared_from_this <T> member function, returning shared_ptr<t>. )

The shared_ptr of the class object itself cannot be stored in the class object itself, otherwise the class object shared_ptr will never be 0, so these resources will never be released unless the program is finished.


Shared_from_this can be used only after shared_ptr<t> constructors have been invoked. The reason is that enable_shared_from_this::weak_ptr is not set in the Enable_shared_from_this<t> constructor, but is set in the Shared_ptr<t> constructor.
A the following code is wrong:
[CPP]View Plain copy print? Class D:public boost::enable_shared_from_this<d> {public:d () {Boost::shared_ptr<d&gt ;       P=shared_from_this ();   }   }; The reason is simple, in the constructor of D, although it is guaranteed that the Enable_shared_from_this<d> constructor has been called, but as mentioned earlier, WEAK_PTR has not been set.
b The following code is also wrong:
[CPP]View Plain copy print? Class D:public boost::enable_shared_from_this<d> {public:void func () {boost::shared_ptr       <D> P=shared_from_this ();   }   };       void Main () {D D;   D.func (); The reason for the error is ditto.
c) The following code is correct:
[CPP]View Plain copy print?       void Main () {boost::shared_ptr<d> d (new D);   D->func (); Here boost::shared_ptr<d> D (new D) actually performs 3 actions:
1. First call the Enable_shared_from_this<d> constructor;
2. Second, call the constructor of D;
3. Finally call the Shared_ptr<d> constructor.
It is the 3rd action that sets the enable_shared_from_this<d> weak_ptr instead of the 1th action. This place is very contrary to C + + commonsense and logic, must be careful.

The conclusion is: Do not use Shared_from_this in the constructor, and secondly, if you want to use shared_ptr, you should use it everywhere, you cannot use D D, and never pass the bare pointers.

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.