c ++ smart pointer introduced _ supplement

Do not understand what I did wrong, these days my wife gave me the cold war again, nor gave me the video let me see the baby. . Hey, tired heart! Taking advantage of some free time tonight to be a complement to the smart pointer right.

 

After writing the article "smart pointer introduced" way to work the next day, suddenly a doubt entrenched in my heart, I feel some defects or omissions article describes.

Question: When two smart pointers reference the same heap obj, then when a smart pointer out of its scope, how do you know another smart pointer referenced heap obj now becomes 1 (use_count)?

           If the reference count is "smart pointer class," a member var inside, then it will disappear as the smart pointer, right? However, a separate smart pointer references another have counted (member var) should not know a Save only ah!

 

After throw this question, review implementation of smart pointers under Android:

        sp <XYZ> sp_obj = new XYZ (); Andrews smart pointer in the referenced class has a requirement: XYZ must inherit from RefBase, implemented in the base class reference count in RefBase.

So XYZ class object is referenced multiple sp_obj_x, because each sp_obj_x can access and modify the inside of XYZ with a class reference counter, so you can easily know when to release the true XYZ class object (heap obj).

 

The answer introduction:

If you want to know the two smart pointers release resources with each other or not, they must be able to "communicate with each other" and therefore "reference counting" between and within the two smart pointers can not be a single class share!

How to make two stack obj to share information it? In addition to heap obj common reference (if inside there is a ref_cnt, also can be done, similar to the Android smart pointer), but only that "reference counting", and it must be dynamic memory!

That is, when the first reference resource (the heap obj), i.e. when the first shared_ptr is constructed, which in addition to a variable to hold the internal stack pointer, the need new piece of memory, this memory for reference count management (ref_cnt inside thereof before adding 1);

When the second referenced by the same resource, i.e. copy configuration (the shared_ptr const the shared_ptr & R & lt  ) ), the reference to which in addition to the common resource, we will also reference the new memory block out last, and plus ref_cnt 1, 2 becomes.

This can be explained after the release of the first smart pointers, the second smart pointer references can know the number of resources managed by the.

 

Other supplements:

1.use_count () corresponding to the sp getStrongCount ().

2.weak_ptr the lock () is equivalent to the wp promote (), to obtain ownership of the resource, and then determine resource availability.

3.get () are used to obtain the resources managed address.

4.reset () and = NULL (ie, operator = ) are released for reference refers to the intelligence resources, but not necessarily really free up resources (while also other smart pointer reference).

5. are overloaded -> and * for resource access its management.

 

Just want to say it, think of it later to add.

 

              

Guess you like

Origin www.cnblogs.com/Dreaming-in-Gottingen/p/11427048.html