5 C++ Boost 智能指標

來源:互聯網
上載者:User

標籤:5 c++ boost 智能指標


智能指標類模板[圖]scoped_ptr[圖]boost scoped_ptr的正確構造boost shared_ptr[圖]boost shared_ptr構建.png[圖]boost shared_ptr可以多次引用指標boost  week ptr[圖]boost week_ptrboost intrusive 侵入式指標boost  make_shared 省略顯式的newboost enable_shared_from_thisboost 循環參考,對象無法析構,記憶體流失boost weak_ptr打破循環參考boost pimpl 介面私人實現技術



智能指標類模板[圖]

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/8B/2A/wKioL1hGVWjjVF_KAAMZ43ZSLK4243.png" title="智能指標類模板.png" alt="wKioL1hGVWjjVF_KAAMZ43ZSLK4243.png" />

scoped_ptr[圖]

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

boost scoped_ptr的正確構造

[email protected]:~/boost $ cat main.cpp // * 操作符重載// ->操作符重載#include <iostream>#include <boost/scoped_ptr.hpp>using namespace std;struct A{A() { cout << "A:A()" << endl;}~A() { cout << "A:~A()" << endl;}void f() { cout << "A:f()" <<endl;}};//智能指標正確的構造方式int main(){boost::scoped_ptr<int>sp(new int(128));cout << ++*sp <<endl;//初始化的方式只有一種,只為獨享//boost::scoped_ptr<int>sp2(sp);//Compile Errorboost::scoped_ptr<A> ap(new A);ap->f();cout << "-------------------" << endl;//示範double free //A a;//棧區變數,離開範圍會自動析構//boost::scoped_ptr<A>(&a);//離開範圍會自動析構//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[圖]

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構建.png[圖]

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/8B/2A/wKioL1hGVaLT6M_8AAOimWrsqEo638.png" title="shared_ptr構建.png" alt="wKioL1hGVaLT6M_8AAOimWrsqEo638.png" />

boost shared_ptr可以多次引用指標

[email protected]:~/boost $ cat main.cpp #include <iostream>#include <boost/shared_ptr.hpp>using namespace std;struct A{        A() { cout << "A:A()" << endl;}        ~A() { cout << "A:~A()" << endl;}        void f() { cout << "A:f()" <<endl;}};//智能指標正確的構造方式int main(){        boost::shared_ptr<int>sp(new int(128));        cout << ++*sp <<endl;        boost::shared_ptr<int>sp2(sp);//shared_ptr可以多次引用        cout << "sp2的引用次數:" << 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的引用次數:2A:A()A:f()-------------------A:~A()[email protected]:~/boost $

boost  week ptr[圖]

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(){        boost::shared_ptr<int>sp(new int(128));        cout << ++*sp <<endl;        boost::shared_ptr<int>sp2(sp);//shared_ptr可以多次引用        cout << "sp2的引用次數:" << sp2.use_count() << endl;        boost::weak_ptr<int> wp(sp2);cout << "wp的引用次數:" <<wp.use_count() << endl;cout << "wp.lock:" <<*wp.lock() << endl;//lock返回一個符合要求的shared_ptr        //wp->f();//weak沒有->操作符重載,編譯報錯        cout << "-------------------" << endl;        return 0;}[email protected]:~/boost $ g++  -Wall main.cpp &&./a.out 129sp2的引用次數:2wp的引用次數:2wp.lock:129-------------------[email protected]:~/boost $

boost intrusive 侵入式指標

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()" << endl;}~D(){cout << "D:~D()" << endl;}};int main(){D* dp = new D;boost::intrusive_ptr<D> dp1(dp);{//局域範圍boost::intrusive_ptr<D> dp2(dp);cout << "dp2 的引用:"<< dp2->getRefCount() << endl;}cout << "dp1 的引用:"<< dp1->getRefCount() << endl;        return 0;}[email protected]:~/boost $ g++  -Wall main.cpp &&./a.out D:D()dp2 的引用:2dp1 的引用:1D:~D()[email protected]:~/boost $


boost  make_shared 省略顯式的new

#include <iostream>#include <boost/shared_ptr.hpp>#include <boost/make_shared.hpp>struct D{D(){ std::cout << "D:D()" << 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=" <<s<< 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>//在一個類的內部使用shared_ptrclass A;void func(boost::shared_ptr<A> p) {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 循環參考,對象無法析構,記憶體流失

[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()" <<std::endl;}~A(){std::cout <<"~A()" <<std::endl;}boost::shared_ptr<B> b;};struct B{B() {std::cout <<"B()" <<std::endl;}~B(){std::cout <<"~B()" <<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打破循環參考

[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()" <<std::endl;}~A(){std::cout <<"~A()" <<std::endl;}boost::shared_ptr<B> b;};struct B{B() {std::cout <<"B()" <<std::endl;}~B(){std::cout <<"~B()" <<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 介面私人實現技術

[email protected]:~/boost $ cat Example.h #ifndef EXAMPLE_H_#define EXAMPLE_H_#include <boost/shared_ptr.hpp>class Example {public:Example();void do_something();private: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::do_something() {std::cout << "use_count() is " << _imp.use_count() << "\n";}[email protected]:~/boost $ cat pimpl_test.cpp #include "Example.h"int main() {    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 $


本文出自 “魂鬥羅” 部落格,謝絕轉載!

5 C++ Boost 智能指標

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.