Use of boost::share_ptr smart Pointers in C++

  • 2020-05-30 20:54:44
  • OfStack

Use of boost::share_ptr smart Pointers in C++

Recently, the smart pointer of boost library was used in the project, and I feel that the smart pointer is quite powerful. Here I post the test code I wrote in the process of learning, for the reference of other friends who want to know about the smart pointer of boost. If there is something wrong, please point out and discuss. Of course, to use the boost smart pointer, you first need to compile the boost library. You can search the boost smart pointer online, so I won't go into details here.

Smart pointer can simplify the development of C++, mainly because it can automatically manage the release of memory, and can do more things, even using smart pointer, you can new code after delete, smart pointer itself will help you manage the release of memory resources.

There are many types of smart Pointers in the Boost library, and the following example demonstrates how to use share_ptr.


/ test.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <vector>
 
/**  The test class   */
class CTest
{
public: 
   
    /**   The constructor  */
    CTest(int m)
    {
        m_member = m;
 
        /**  To apply for space  */ 
        m_pname = new char[2];
    }
    /**  The destructor  */
    ~CTest()
    {
        delete m_pname;
    }
 
    /**  A member function  */ 
    int getMember()
    {
        return m_member;
    }
 
private:
 
    /**  Data members  */ 
    int m_member;
    char * m_pname;
   
};
 
int _tmain(int argc, _TCHAR* argv[])
{
 
/**  Sample code [ 1 】  */ 
 
    /** boost::shared_ptr Smart pointer contains 1 Individual reference counter  */ 
    /**  The reference pointer counter records how many reference Pointers to the same 1 Two objects, if at the end 1 When a reference pointer is destroyed, the object itself is destroyed.  */ 
 
    /**  Create using smart Pointers 1 An object  */
    /**  Pay attention to :  Smart Pointers do not support directness  new  For example, : boost::shared_ptr<CTest> pTemp = new CTest(2)  It is wrong to  */ 
    boost::shared_ptr<CTest> pTemp(new CTest(10));
 
    /**  create 1 A new smart pointer also points to the newly created CTest object  */ 
    /**  Smart pointer supports equal sign operation  */ 
    boost::shared_ptr<CTest> pSecond = pTemp;
 
    /**  Access the object through a smart pointer  */ 
    std::cout << pTemp->getMember() << std::endl;
 
    /**  For the first 1 All smart Pointers are null , No longer points to the object , Pay attention to , Smart pointer cannot be used  pTemp = NULL */
    pTemp.reset();
 
    /**  For the first 2 All smart Pointers are also null , At this moment the CTest The object no longer has a smart pointer to it , It will destruct automatically  */ 
    pSecond.reset();
 
/**  Sample code [ 2 】  */ 
 
    /**  will 1 Two ordinary Pointers are converted to smart Pointers  */
 
    /**  create 1 Ordinary Pointers ,new1 An object  */ 
    CTest * pATest = new CTest(100);
    /**  Convert to smart pointer  */ 
    boost::shared_ptr<CTest> pShareTest(pATest);
 
    /**  The smart pointer will manage the creation automatically CTest object , No more delete, Otherwise the program will die  */ 
    delete pATest;
 
    /**  Make the smart pointer null , The object will be destructed automatically  */ 
    pShareTest.reset();
 
 
/**  Sample code [ 3 】  */ 
 
    /**  create 1 Three containers hold smart Pointers  */
    /**  So notice here :  The two"  >  "   Don't write 1 Yes, otherwise it will happen  >>  Operator overloading   */ 
    std::vector<boost::shared_ptr<CTest> > vec;
 
    /**  create 1 A temporary CTest object , Store in the top container  */ 
 
    {
        /**  Create using smart Pointers 1 An object  */
        boost::shared_ptr<CTest> pTemp(new CTest(2));
 
        /**  Add to container  */ 
        vec.push_back(pTemp);
 
        /**  Leave the curly braces , the pTemp destructor , So only the pointer in the container points to the newly created one CTest */ 
    }
 
    /**  let vector The iterator points to that push_back Smart pointer to the container  */ 
    std::vector<boost::shared_ptr<CTest> >::iterator itor = vec.begin();
 
    /**  Access smart pointer , (*itor) Is a smart pointer object , It points to what we just created CTest, through  ->  Methods to access CTest object   */ 
    std::cout << (*itor)->getMember()<<std::endl;
 
 
    /**  Empty container , When the container is emptied , The smart pointer in the container is deleted ,
         There is no smart pointer to the object at this point , Therefore, the CTest The object will be destructed automatically  */ 
    vec.clear();
 
    int temp;
    std::cin >> temp;
 
 
    return 0;
}
 
/**  There are some caveats to using smart Pointers  */ 
 
// 1.  Smart Pointers are 1 Kinds of objects , It's not just Pointers , So when the smart pointer contains another 1 Object of class ,
//     Need to include another 1 Class header file , You cannot simply use forward reference declarations 
// eg: CMyClass.h file 
 
 #include "CTest.h"
 /**  You cannot simply use a forward reference declaration , Header files must be included  */ 
 //  Forward reference declaration  class CTest
 
class CMyClass
{
public:
 
private:
    boost::shared_ptr<CTest> m_pTest;
 
};
 
// 2. shared_ptr  It's thread safe 
 
// 3.  Circular references occur with smart Pointers 
//  That is, two classes contain smart Pointers to objects of the other class 

If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!


Related articles: