AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

C++智能指針enable_shared_from_this

來源: 責編: 時間:2023-11-17 17:14:06 322觀看
導讀enable_shared_from_this介紹enable_shared_from_this其實是智能指針中的內容,它的作用就是用于在類的內部,返回一個this的智能指針。對于enable_shared_from_this,初學者可能不明白它的使用場景和使用的必要性,可能有得

pWP28資訊網——每日最新資訊28at.com

enable_shared_from_this介紹

enable_shared_from_this其實是智能指針中的內容,它的作用就是用于在類的內部,返回一個this的智能指針。pWP28資訊網——每日最新資訊28at.com

對于enable_shared_from_this,初學者可能不明白它的使用場景和使用的必要性,可能有得童鞋們會問既然有了this這個指向自己的指針, 為什么還需要enable_shared_from_this這個東西呢,直接用this代替不就好了嗎?pWP28資訊網——每日最新資訊28at.com

我們來看看以下代碼例子,如果先不運行,你能看出什么問題嗎?pWP28資訊網——每日最新資訊28at.com

#include <iostream>class Person{public:    Person() = default;    ~Person(){    };    std::shared_ptr<Person> getPtr(){        return std::shared_ptr<Person>(this);    }};int main() {    std::shared_ptr<Person> person = std::make_shared<Person>();    std::shared_ptr<Person> person1 = person->getPtr();    std::cout << "person.use_count() = " << person.use_count() << std::endl;    std::cout << "person1.use_count() = " << person1.use_count() << std::endl;    return 0;}

以上代碼運行崩潰報錯了,這是為什么呢?pWP28資訊網——每日最新資訊28at.com

pWP28資訊網——每日最新資訊28at.com

崩潰信息pWP28資訊網——每日最新資訊28at.com

這是因為只有一個Person的指針,但是卻被兩個智能指針shared_ptr持有,而它們的引用計數都是1,因此當main函數運行完畢后兩個智能指針釋放時都對同一個Person指針進行釋放導致的崩潰。pWP28資訊網——每日最新資訊28at.com

如果我們能讓兩個智能指針shared_ptr共享同一個引用計數,那么這個崩潰問題就迎刃而解了。而通過讓Person繼承基類enable_shared_from_this,然后在函數getPtr中 調用基類的shared_from_this就能返回一個this的智能指針,這樣即可實現讓多個智能指針共享同一個引用計數,而達到銷毀時只釋放一次的目的。這就是enable_shared_from_this存在的必要性, 這也是this無法替代的功能點。pWP28資訊網——每日最新資訊28at.com

如下是實例代碼:pWP28資訊網——每日最新資訊28at.com

#include <iostream>class Person:public std::enable_shared_from_this<Person>{public:    Person() = default;    ~Person(){    };    std::shared_ptr<Person> getPtr(){        return shared_from_this();    }};int main() {    std::shared_ptr<Person> person = std::make_shared<Person>();    std::shared_ptr<Person> person1 = person->getPtr();    std::cout << "person.use_count() = " << person.use_count() << std::endl;    std::cout << "person1.use_count() = " << person1.use_count() << std::endl;    return 0;}

通過運行調試打印,我們可以看到這person和person1這兩個智能指針的引用計數都變為了2,這是正確的。pWP28資訊網——每日最新資訊28at.com

pWP28資訊網——每日最新資訊28at.com

通過兩個實例代碼的對比,我們可以發現問題的根源所在就是我們在返回this的智能指針時,直接調用std::shared_ptr構造函數傳入裸指針的方式構造一個智能指針, 而在之前的介紹中我們提到過使用智能指針shared_ptr時盡量使用std::make_shared進行智能指針的構造,避免直接調用std::shared_ptr構造函數傳入裸指針的方式進行構造。pWP28資訊網——每日最新資訊28at.com

更多關于enable_shared_from_this的實踐對比可以參照官網學習:https://en.cppreference.com/w/cpp/memory/enable_shared_from_thispWP28資訊網——每日最新資訊28at.com

enable_shared_from_this的實現

我們通過源碼的方式來分析下enable_shared_from_this的實現原理,enable_shared_from_this的源碼非常簡短:pWP28資訊網——每日最新資訊28at.com

template<class _Tp>class _LIBCPP_TEMPLATE_VIS enable_shared_from_this{    mutable weak_ptr<_Tp> __weak_this_;protected:    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR    enable_shared_from_this() _NOEXCEPT {}    _LIBCPP_INLINE_VISIBILITY    enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}    _LIBCPP_INLINE_VISIBILITY    enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT        {return *this;}    _LIBCPP_INLINE_VISIBILITY    ~enable_shared_from_this() {}public:    _LIBCPP_INLINE_VISIBILITY    shared_ptr<_Tp> shared_from_this()        {return shared_ptr<_Tp>(__weak_this_);}    _LIBCPP_INLINE_VISIBILITY    shared_ptr<_Tp const> shared_from_this() const        {return shared_ptr<const _Tp>(__weak_this_);}#if _LIBCPP_STD_VER > 14    _LIBCPP_INLINE_VISIBILITY    weak_ptr<_Tp> weak_from_this() _NOEXCEPT       { return __weak_this_; }    _LIBCPP_INLINE_VISIBILITY    weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT        { return __weak_this_; }#endif // _LIBCPP_STD_VER > 14    template <class _Up> friend class shared_ptr;};

通過源碼我們可以發現這是一個模版類,將自身類型以模版參數的形式傳入到父類,這是典型的CRTP應用,關于CRTP之前我們已經介紹過了,這里不再累贅。感興趣的童鞋們可以參考之前的博文:pWP28資訊網——每日最新資訊28at.com

C++之CRTP的使用

enable_shared_from_this對外只提供了一個weak_from_this公共方法,其內部通過以為弱引用的智能指針weak_ptr構造了一個shared_ptr,這里并沒有什么問題, 問題這個弱引用的智能指針__weak_this_它是在哪里初始化的呢?我們通shared_ptr的構造函數可以發現,如果傳入的weak_ptr沒有初始化的話是會拋出異常崩潰的。pWP28資訊網——每日最新資訊28at.com

其實成員變量__weak_this_的初始化是在類的外部進行初始化的,它的奧秘就是源碼的倒數第二行template ();改為不使用智能指針, 而使用裸指針的方式,修改為 auto person = new Person;,同時注釋掉第16行再運行是會崩潰的,這就是因為__weak_this_沒有進行初始化的原因。pWP28資訊網——每日最新資訊28at.com

pWP28資訊網——每日最新資訊28at.com

崩潰信息pWP28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-27970-0.htmlC++智能指針enable_shared_from_this

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: Python系列:如何提高python程序代碼的健壯性

下一篇: C++中的低級內存操作

標簽:
  • 熱門焦點
Top