Learning Boost 5 - Smart Ptr

xiaoxiao2021-03-06  41

Learning Boost 5

Smart Ptr

The top 5 smart pointers provided in Boost. They are:

l Scoped_PTR exclusive pointer

l Scoped_Array exclusive array

l Shared_PTR reference count pointer

l Shared_Array reference count array

l WEAK_PTR Shared_PTR's weak pointer (not participating in reference count)

l Intrusive_PTR Custom reference count pointer

Scoped_ptr and scoped_array

For scoped_ptr and scoped_array, it is not possible to copy construct and assignment (copy constructor and Operator = all private members). So they don't pass the ownership of the pointer as Auto_Ptr, but exclusively owned the right, until the destruction of the object (when Scoped_PTR, the Scropted_Array destructor, the object to be directed).

structure:

Scoped_ptr a (new int);

Scoped_ptr B;

Scoped_Array C (New Int ");

Scoped_Array D;

Scoped_ptr uses new T or NULL to initialize the object. Scoped_array uses new T [] or NULL to initialize the object.

The default constructor is initialized using null.

access:

Scoped_ptr Heavy loaded Operator * and Operator-> operators, and Scoped_Array heavyloaded the Operator [] subscript operator. Use these operators to operate the objects you point to. They can get the original pointer through the T * GET () member function.

E.g:

Scoped_Array Str (New Char [100]);

STRCPY (Str.get (), "Hello");

COUT << STR [0] << Str [4] << endl; // Output HO

Void Reset (T * P = 0)

Reset is used to reset the object pointing to the pointer. When the RESET is called, scoped_ptr or scoped_array will first delete the original pointing object (if not point to null). The Reset parameters and their constructor are the same, must be new T (or new t []) or NULL.

SWAP

Swap global functions and SWAP member functions, swap two objects that two smart pointers. E.g:

Scoped_ptr a (new int); // a pointing to an int

Scoped_ptr b; // b pointing NULL

SWAP (A, B); // At this point, a point to NULL, B pointing to an int

Bool

Scoped_ptr and scoped_array can be implicit to the Bool type, returning false when pointing null, otherwise returns true.

Types of

Type T in Scoped_PTR and Scoped_Array cannot be a Void type.

And scoped_ptr and scoped_ptr do not exist, even if T and u can be converted, scoped_ptr and scoped_ptr cannot be converted.

Shared_ptr and Shared_Array

Shared_ptr and Shared_Array are intelligent pointers that are referenced.

Let's only describe the use of Shared_PTR.

structure:

There are 6 constructor for Shared_Ptr :

Shared_ptr (Y * P);

The most common constructor, P must be a new y of the pointer or NULL. Note that the type of P here is Y *, rather than T * in Shared_PTR , so P must have a type conversion to T *. At the same time, Shared_PTR will remember that the type P is y. When the object points to P, Shared_PTR uses the correct destructor (Y-type destructor, rather than the Type of destructor). E.g:

Struct a

{

A () {cout << "a construct" << endl;

~ A () {cout << "a destory" << endl;

}

int main ()

{

Shared_ptr a (new a);

Return 0;

}

/ *

Program output:

A construct

A destroy

This shows that a correct destructor is not destructed because of Shared_Ptr , for example:

Void

Del

(void * p) {delete p;} This is wrong

* /

Shared_ptr (Y * P, D D);

Ibid. D is an imitation function that destructure P, for example:

Template

Void my_delete (t * p)

{

Delete P;

}

int main ()

{

Shared_ptr a (New A, My_DELETE

Return 0;

}

Shared_ptr (Shared_Ptr Const & R);

Copy constructor, pay attention to the type conversion.

Shared_ptr (Weak_Ptr & R);

Although Weak_Ptr does not participate in the reference count, Shared_PTR will.

Shared_ptr (std :: auto_ptr & r);

At this time, r points to NULL.

Reset reset:

Void reset ();

Void Reset (Y * P);

Void Reset (Y * P, D D);

Shared_ptr & Operator = (Shared_Ptr Const & R);

Shared_ptr & Operator = (Shared_Ptr Const & R);

Shared_ptr & Operator = (std :: auto_ptr & r);

Make the object reference count of the original Shared_PTR to one and points to another object, similar to the reconstruction, so their parameters and constructors are the same.

Quote Count members function:

Long use_count (); return reference count

BOOL UNIQUE (); Return Use_count () == 1

other:

Bool type conversion, operator ==, operator! =, operator <, swap, get, stream output (output pointer address) D * get_deleter () gets the deleter, used to analyze the imitation function pointer of the object.

Type conversion:

Shared_ptr static_pointer_cast (Shared_Ptr const & r);

Shared_ptr const_pointer_cast (Shared_Ptr const & r);

Shared_ptr Dynamic_pointer_cast (Shared_Ptr const & r);

The above is some functions similar to the C type conversion. Since the Shared_PTR supports the type conversion of the pointer, Shared_PTR can save the VOID pointer or to the base class pointer. This, scoped_ptr is not!

WEAK_PTR

Weak_ptr is an accessor for Shared_PTR, and Weak_ptr does not participate in the reference count of Shared_PTR, nor does it delete the pointed object. At the same time, when the object is deleted by Shared_PTR, Weak_ptr is invalid.

The reason why you use WEAK_PTR without using T *, which can be converted to Shared_PTR participation reference count, while t * is not (pointer only of NEW T can construct Shared_PTR). That is, though WEAK_PTR does not participate in the reference count, the reference count does still exist, and the mechanism is transmitted to Shared_PTR when it is necessary to convert to Shared_PTR. Otherwise, use T * to construct Shared_PTR, equivalent to a two-set mechanism to manage reference count, then a Shared_PTR may point to invalid objects (object deleted by another set of reference counting mechanism).

structure:

Weak_ptr (); // Constructed Weak_ptr pointing NULL

WEAK_PTR (Weak_Ptr Const & R); // Copy Construction Function

Weak_ptr (Weak_Ptr Const & R); // Type Conversion Constructor

Weak_ptr (Shared_Ptr Const & R); // Through Shared_PTR Construction, support type conversion

Weak_ptr & Operator = (Weak_Ptr Const & R); // Reserved Operator

Weak_ptr & Operator = (Weak_Ptr "; // Type Conversion

Weak_ptr & operator = (Shared_Ptr Const & R); //. . .

Convert to Shared_PTR:

Shared_ptr lock (); // Lock member function, constructed Shared_Ptr through Weak_ptr

Or you can use Shared_PTR constructor. Please refer to the constructor of SHARED_PTR above.

Quote count:

Long use_count (); // Returns the reference count, although not involved in the reference count, but still can

BOOL Expired (); // Returns USE_COUNT () == 0 Others:

Void reset (); // Reset to NULL

SWAP member function, SWAP global function, Operator

Intrusive_ptr

Intrusive_PTR literally means an invasive pointer, in fact, it is defined by reserving the mechanism of the reference count to the user. For example, the COM object in Windows has its own set of reference counting mechanisms, then INTRUSIVE_PTR can be used to manage the reference count, and Intrusive_PTR will increase and reduce the reference count when appropriate.

Use INTRUSIVE_PTR to customize the following two functions:

Void Intrusive_PTR_ADD_REF (T * P); // Intrusive_PTR call when adding a reference count

Void Intrusive_ptr_release (t * p); // Intrusive_PTR call when the reference count is reduced

I don't have much to say for Intrusive_Ptr, please see an example below.

#include

#include

Using namespace std;

USING NAMESPACE BOOST;

Struct MyoBJ

{

INT * REF_COUNT; / / Reference count

Myobj ()

{

REF_COUNT = New INT (0);

Cout << "Myobj Construct" << Endl;

}

~ Myobj ()

{

Cout << "myobj destory" << Endl;

}

}

Void Intrusive_PTR_ADD_REF (MyObj * P)

{

Cout << "add ref" << Endl;

(* (p-> ref_count));

}

Void intrusive_ptr_release (myobj * p)

{

COUT << "release" << endl;

IF (* (p-> ref_count) == 0)

Delete P;

}

Void Use (Intrusive_Ptr Obj) // USING OBJ

{

}

int main ()

{

Intrusive_ptr obj (new myobj);

Intrusive_ptr obj2 = OBJ;

Use (OBJ2);

Return 0;

}

Summary

In addition to Scoped_PTR and Scoped_array, it can be used as a member of the STL container.

In addition to the member function T * get (), you can use the global function GET_POINTER to get the original pointer.

T * get_pointer (Scoped_Ptr const & P); etc.

You can implicitly convert to Bool types.

Etc., etc. . . .

转载请注明原文地址:https://www.9cbs.com/read-63897.html

New Post(0)