5 C + + Boost smart pointer

Source: Internet
Author: User


Smart pointer class template [figure]scoped_ptr[figure]boost scoped_ptr correct construction boost shared_ptr[diagram]boost shared_ptr build. png[Diagram]boost shared_ PTR can reference pointers multiple times boost week ptr[figure]boost week_ptrboost intrusive intrusive pointer boost make_shared omit explicit newboost enable_shared_from_ Thisboost loop reference, object not destructor, memory leak boost weak_ptr break loop reference boost Pimpl interface private implementation Technology



Smart pointer class template [diagram]

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/8B/2A/wKioL1hGVWjjVF_KAAMZ43ZSLK4243.png "title=" Smart pointer class template. png "alt=" Wkiol1hgvwjjvf_kaamz43zslk4243.png "/>

scoped_ptr[Map]

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/8B/2A/wKioL1hGVXfyhuY4AAT6_DtIhv8949.png "title=" scoped _ptr.png "alt=" Wkiol1hgvxfyhuy4aat6_dtihv8949.png "/>

The correct construction of boost scoped_ptr

[email protected]:~/boost $ cat main.cpp // *  operator Overloading// -> operator overloading # Include <iostream> #include  <boost/scoped_ptr.hpp>using namespace std;struct  a{a ()  { cout <<  "a:a ()" &NBSP;&LT;&LT;&NBSP;ENDL;} ~a ()  { cout <<  "A:~a ()" &NBSP;&LT;&LT;&NBSP;ENDL;} Void f ()  { cout <<  "a:f ()"  <<endl;}};/ /smart pointer correct construction mode Int main () {boost::scoped_ptr<int>sp (new int); cout << ++*sp  <<endl;//initialization is only one way, only for exclusive//BOOST::SCOPED_PTR&LT;INT&GT;SP2 (SP);//compile errorboost::scoped_ Ptr<a> ap (New a), ap->f ();cout <<  "-------------------"  <<  endl;//demonstrates the double free //a a;//stack area variable, leaving the scope to automatically refactor//boost::scoped_ptr<a> (&a);//Leave scope automatically destructors// compile error:note: previous declaration as  ' A a ' return 0;} [email protected]:~/boost $ g++  -wall main.cpp &&./a.out 129a:a () A:f ()--------------- ----A:~a () [email protected]:~/boost $

Boost Shared_ptr[Chart]

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/8B/2D/wKiom1hGVaHTxZo7AAbp45J7yYE764.png "title=" shared _ptr.png "alt=" Wkiom1hgvahtxzo7aabp45j7yye764.png "/>


Boost shared_ptr build. png[Map]

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/8B/2A/wKioL1hGVaLT6M_8AAOimWrsqEo638.png "title=" shared _ptr build. png "alt=" Wkiol1hgvalt6m_8aaoimwrsqeo638.png "/>

boost shared_ptr can refer to the pointer multiple times

[email protected]:~/boost $ cat main.cpp  #include  <iostream> #include   <boost/shared_ptr.hpp>using namespace std;struct A{         a ()  { cout <<  "a:a ()" &NBSP;&LT;&LT;&NBSP;ENDL;}         ~a ()  { cout <<  "A:~a ()"  < < endl;}         void f ()  { cout <<  "a:f ()"  <<endl;}};/ /smart pointer correct construction mode Int main () {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BOOST::SHARED_PTR&LT;INT&GT;SP (new  int (+));        cout << ++*sp << ENDL;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BOOST::SHARED_PTR&LT;INT&GT;SP2 (SP);//shared_ptr can be referenced multiple times         cout <<  "SP2 citations:"  << sp2.use _count ()  << endl;                 boost::shared_ptr<a> ap (new a);         ap- >f ();        cout <<  "-------------------"   << endl;        return 0;} [email protected]:~/boost $ g++  -wall main.cpp &&./a.out  129SP2 citations: 2a:a () a:f ()-------------------a:~a () [email protected]:~/boost $

Boost Week ptr[chart]

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/8B/2D/wKiom1hGVcyzLbV9AAbkUWN1HEI403.png "title=" Week_ Ptr.png "alt=" Wkiom1hgvcyzlbv9aabkuwn1hei403.png "/>

Boost Week_ptr

[email protected]:~/boost $ cat main.cpp  #include  <iostream> #include   <boost/shared_ptr.hpp> #include  <boost/weak_ptr.hpp>using namespace std;int  Main () {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BOOST::SHARED_PTR&LT;INT&GT;SP (New int (128));         cout << ++*sp <<endl;   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BOOST::SHARED_PTR&LT;INT&GT;SP2 (SP);//shared_ptr can be quoted multiple times          cout <<  "SP2 citations:"  << sp2.use_count ()  < &LT;&NBSP;ENDL;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BOOST::WEAK_PTR&LT;INT&GT;&NBSP;WP (SP2); cout  <<  "WP citations:"  <<wp.use_count ()  << endl;cout <<  " Wp.lock: " <<*wp.lock ()  << endl;//lock returns a compliant shared_ptr         //wp-&Gt;f ();//weak do not have operator overloading, compile error         cout <<  "------- ------------"&NBSP;&LT;&LT;&NBSP;ENDL;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;0;} [email protected]:~/boost $ g++  -wall main.cpp &&./a.out  129SP2 citations: 2wp citations: 2wp.lock:129-------------------[email protected]:~/boost $

Boost intrusive intrusive pointer

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/8B/2D/wKiom1hGVfXCqO2YAAd0n9BGTp0994.png "title=" Intrusive_ptr.png "alt=" Wkiom1hgvfxcqo2yaad0n9bgtp0994.png "/>

[email protected]:~/boost $ cat main.cpp  #include  <iostream> #include   <boost/shared_ptr.hpp> #include  <boost/intrusive_ptr.hpp>using namespace std;class  referencecounter{public:referencecounter (): RefCount (0) {}virtual ~referencecounter () {}friend  Void intrusive_ptr_add_ref (referencecounter *p) {++p->refcount;} Friend void intrusive_ptr_release (referencecounter *p) {if (--p->refcount == 0) { Delete p;}} Int getrefcount ()  const{return refcount;} private:int refcount;}; Class d:public referencecounter{public:d () {cout <<  "D:D ()" &NBSP;&LT;&LT;&NBSP;ENDL;} ~d () {cout <<  "d:~d ()"  << endl;}}; Int main () {D*&NBSP;DP&NBSP;=&NBSP;NEW&NBSP;D;BOOST::INTRUSIVE_PTR&LT;D&GT;&NBSP;DP1 (DP); { Reference to local scope BOOST::INTRUSIVE_PTR&LT;D&GT;&NBSP;DP2 (DP);cout <<  "dp2 :" << dp2- >getrefcount ()  << endl;} cout <<  "dp1  Quote:" << dp1->getrefcount ()  << endl;         return 0;} [Email protected]:~/boost $ g++  -wall main.cpp &&./a.out d:d ( Reference to the dp2  reference:2dp1 : 1d:~d () [email protected]:~/boost $


Boost Make_shared omit the explicit new

#include  <iostream> #include  <boost/shared_ptr.hpp> #include  <boost/make_ Shared.hpp>struct d{d () { std::cout <<  "d:d ()" &NBSP;&LT;&LT;&NBSP;STD::ENDL;} D (int k,double d) {std::cout <<  "k=" <<k<< " d=" << d  << std::endl;} ~d () {std::cout <<  "d:~d ()"  << std::endl;}}; Struct e{e (std::string s)  {std::cout <<  "s=" &NBSP;&LT;&LT;S&LT;&LT;&NBSP;STD:: Endl;} ~e () {std::cout <<  "e:~e ()"  << std::endl;}}; Int main () {boost::shared_ptr<d> dp1 = boost::make_shared<d> (); boost::shared_ Ptr<d> dp2 = boost::make_shared<d> (12,3.14); boost::shared_ptr<e> ep1  = boost::make_shared<E> ("Boost");        return  0;} [email protected]:~/boost $ g++  -wall  Main.cpp &&./a.out d:d () k=12 d=3.14s=booste:~e () d:~d () d:~d () [email protected]:~/ boost $

boost enable_shared_from_this

 #include  <boost/shared_ptr.hpp> #include  <boost/enable_ shared_from_this.hpp>//using Shared_ptrclass a;void func (BOOST::SHARED_PTR<A>&NBSP;P) inside a class  {std::cout <<  "sizeof (P) =" <<sizeof (p)  <<std::endl;} Class a:public boost::enable_shared_from_this<a>{public:void f () {std::cout <<   "a f ()  \n"; Func (Shared_from_this ());} Void say_hi () {std::cout <<  "hello boost \n";}; Int main () {boost::shared_ptr <a> a (new a); A->f ();         return 0;} [Email protected]:~/boost $ g++  -wall main.cpp &&./a.out a  f ()  sizeof (p) =8[email protected]:~/boost $ 

boost Circular reference, object not destructor, memory leak

[email protected]:~/boost $ cat main.cpp  #include  <iostream> #include   <boost/shared_ptr.hpp> #include  <boost/enable_shared_from_this.hpp>struct b;struct a {A ()  {std::cout << "A ()" &NBSP;&LT;&LT;STD::ENDL;} ~a () {std::cout << "~a ()" &NBSP;&LT;&LT;STD::ENDL;} boost::shared_ptr<b> b;}; Struct b{b ()  {std::cout << "B ()" &NBSP;&LT;&LT;STD::ENDL;} ~b () {std::cout << "~b ()" &NBSP;&LT;&LT;STD::ENDL;} boost::shared_ptr<a> a;}; Int main () {boost::shared_ptr<a> ap (new a); boost::shared_ptr<b> bp (New B); ap->b = bp;bp->a = ap;std::cout <<  "------------\ n";         return 0;} [Email protected]:~/boost $ g++  -wall main.cpp &&./a.out a () B ()------------[email protected]:~/boost $

boost weak_ptr Break Circular reference

[email protected]:~/boost $ cat main.cpp  #include  <iostream> #include   <boost/shared_ptr.hpp> #include  <boost/enable_shared_from_this.hpp>struct b;struct a {A ()  {std::cout << "A ()" &NBSP;&LT;&LT;STD::ENDL;} ~a () {std::cout << "~a ()" &NBSP;&LT;&LT;STD::ENDL;} boost::shared_ptr<b> b;}; Struct b{b ()  {std::cout << "B ()" &NBSP;&LT;&LT;STD::ENDL;} ~b () {std::cout << "~b ()" &NBSP;&LT;&LT;STD::ENDL;} boost::shared_ptr<a> a;boost::weak_ptr<a> a;}; Int main () {boost::shared_ptr<a> ap (new a); boost::shared_ptr<b> bp (New B); ap->b = bp;bp->a = ap;std::cout <<  "------------\ n";         return 0;} [Email protected]:~/boost $ g++  -wall main.cpp &&./a.out a () B ()------------~a () ~b () [Email protected]:~/boost $ 

boost Pimpl Interface Private Implementation technology

[email protected]:~/boost $ cat example.h  #ifndef  example_h_#define example _h_#include <boost/shared_ptr.hpp>class example {public:example (); Void do_something ( );p rivate:class implementation;// hide implementation detailsboost::shared_ptr< implementation> _imp;}; #endif  /* example_h_ */[email protected]:~/boost $ cat example.cpp # include <iostream> #include   "Example.h" class example::implementation {public:~ Implementation ()  {std::cout <<  "destroying implementation\n";}; Example::example (): _imp (new implementation)  {}void example::d o_something ()  {std::cout  <<  "Use_count ()  is "  << _imp.use_count ()  <<  "\ n";} [email protected]:~/boost $ cat pimpl_test.cpp  #include   "Example.h" Int main ()  {  &Nbsp; example a;    a.do_something ();     example b (a);     b.do_something ();     example c;    c  = a;    c.do_something ();     return 0;} [email protected]:~/boost $ g++  -wall pimpl_test.cpp example.cpp   &&./a.out use_count ()  is 1use_count ()  is 2destroying  Implementationuse_count ()  is 3destroying implementation[email protected]:~/boost $


This article from "Soul Bucket Luo" blog, declined reprint!

5 C + + Boost smart pointer

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.