Boost smart pointer

Source: Internet
Author: User

Address: http://ticktick.blog.51cto.com/823160/196765

Recently, when I used the boost library's smart pointer in my project, I felt that the smart pointer was quite powerful. Here I posted my own tests written during the learning process.CodeFor the reference of other friends who want to know about boost smart pointers. If you have made any mistakes, please point out the discussion. Of course, to use the boost smart pointer, you must first compile the boost library. The specific method can be queried on the Internet and will not go into details here.

Smart pointers simplify the development of C ++, mainly because they can automatically manage the release of memory and do more, that is, using smart pointers, then you can use the new code without the delete operation. The smart pointer will help you manage the release of memory resources.

There are many types of smart pointers for the boost library. The following uses the sample code to describe how to use javas_ptr.

// Test. cpp: defines the entry point for the console application.
# Include "Stdafx. H"
# Include <iostream>
# Include <boost/shared_ptr.hpp>
# Include <vector>

/** Test class */
Class Ctest
{
Public :

/** Constructor */
Ctest ( Int M)
{
M_member = m;

/** Apply for Space */
M_pname = New Char [2];
}
/** Destructor */
~ Ctest ()
{
Delete m_pname;
}

/** Member functions */
Int Getmember ()
{
Return M_member;
}

Private :

/** Data member */
Int M_member;
Char * M_pname;

};

Int _ Tmain ( Int Argc, _ tchar * argv [])
{

/** Sample code [1 ]*/

/** Boost: The shared_ptr smart pointer contains a reference counter */
/** Reference pointer counter records the number of reference pointers pointing to the same object. If the last reference pointer is destroyed, the object itself is destroyed. */

/** Use a smart pointer to create an object */
/** Note: Smart pointers do not support direct New For example: boost: shared_ptr <ctest> ptemp = new ctest (2 */
Boost: shared_ptr <ctest> ptemp ( New Ctest (10 ));

/** Create a new smart pointer and point it to the newly created ctest object */
/** Smart pointer supports equal sign operation */
Boost: shared_ptr <ctest> partition cond = ptemp;

/** Access this object using a smart pointer */
STD: cout <ptemp-> getmember () <STD: Endl;

/** Leave the first smart pointer empty and no longer point to this object. Note that the smart pointer cannot use ptemp = NULL */
Ptemp. Reset ();

/** Leave the second smart pointer empty. Then the ctest object has no smart pointer pointing to it and will automatically analyze the structure */
Using cond. Reset ();

/** Sample code [2 ]*/

/** Convert a common pointer to a smart pointer */

/** Create a common pointer and a new object */
Ctest * patest = New (Ctest (100 );
/** Convert to smart pointer */
Boost: shared_ptr <ctest> psharetest (patest );

/** The smart pointer automatically manages the created ctest object and does not allow delete operations. OtherwiseProgramWill be suspended */
Delete patest;

/** If the smart pointer is null, the object will be automatically destructed */
Psharetest. Reset ();


/** Sample code [3 ]*/

/** Create a container to store smart pointers */
/** Note: Do not write the two ">" together. Otherwise,> operator overload is generated */
STD: vector <boost: shared_ptr <ctest> VEC;

/** Create a temporary ctest object and store it in the above container */

{
/** Use a smart pointer to create an object */
Boost: shared_ptr <ctest> ptemp ( New Ctest (2 ));

/** Add to container */
VEC. push_back (ptemp );

/** Exit the braces, then the ptemp destructor, so only the pointer in the container points to the newly created ctest */
}

/** Point the vector iterator to the smart pointer just pushed back to the container */
STD: vector <boost: shared_ptr <ctest> >:: iterator itor = Vec. Begin ();

/** Access the smart pointer (* itor) refers to the smart pointer object, pointing to the created ctest, and accessing the ctest object through the-> Method */
STD: cout <(* itor)-> getmember () <STD: Endl;


/** Clear the container. when the container is cleared, the smart pointer in the container is deleted,
Because no smart pointer points to this object at this time, the ctest object will automatically analyze */

VEC. Clear ();

Int Temp;
STD: CIN> temp;


Return 0;
}

/** Notes for using smart pointers */

// 1. A smart pointer is actually a type of object and is not a simple pointer. Therefore, when a smart pointer contains an object of another class,
// The header file of another class must be included, and the forward reference declaration cannot be used simply.
// Eg: cmyclass. h file

# Include "ctest. H"
/** The forward reference declaration cannot be simply used. The header file must be included */
// Declare class ctest TO THE REFERENCE

Class Cmyclass
{
Public :

Private :
Boost: shared_ptr <ctest> m_ptest;

};

// 2. shared_ptr is thread-safe

// 3. Use a smart pointer for cyclic reference
// The two classes contain the smart pointer of the other class object.
// Read the following article.Article BoostSmart pointer -- weak_ptr

URL:
Http://www.cnblogs.com/TianFang/archive/2008/09/20/1294590.html

End

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.