成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C++智能指針enable_shared_from_this

開發 前端
enable_shared_from_this其實是智能指針中的內容,它的作用就是用于在類的內部,返回一個this的智能指針。

enable_shared_from_this介紹

enable_shared_from_this其實是智能指針中的內容,它的作用就是用于在類的內部,返回一個this的智能指針。

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

我們來看看以下代碼例子,如果先不運行,你能看出什么問題嗎?

#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;
}

以上代碼運行崩潰報錯了,這是為什么呢?

崩潰信息

這是因為只有一個Person的指針,但是卻被兩個智能指針shared_ptr持有,而它們的引用計數都是1,因此當main函數運行完畢后兩個智能指針釋放時都對同一個Person指針進行釋放導致的崩潰。

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

如下是實例代碼:

#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,這是正確的。

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

更多關于enable_shared_from_this的實踐對比可以參照官網學習:https://en.cppreference.com/w/cpp/memory/enable_shared_from_this

enable_shared_from_this的實現

我們通過源碼的方式來分析下enable_shared_from_this的實現原理,enable_shared_from_this的源碼非常簡短:

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之前我們已經介紹過了,這里不再累贅。感興趣的童鞋們可以參考之前的博文:

C++之CRTP的使用

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

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

崩潰信息

責任編輯:趙寧寧 來源: 思想覺悟
相關推薦

2010-02-05 14:36:20

C++智能指針

2010-12-17 10:07:59

2024-12-26 10:45:08

2021-09-09 17:05:36

C++智能指針語言

2023-12-20 12:40:51

C++RAII編程

2025-02-26 01:23:02

C++11Raw代碼

2024-01-24 11:44:44

C++智能指針開發

2015-07-27 11:34:03

Linux內核指針

2024-03-01 16:43:48

C++11智能指針內存

2010-01-27 14:18:41

Android智能指針

2021-08-11 09:01:48

智能指針Box

2021-12-21 15:31:10

C++語言指針

2021-07-29 06:09:05

萬能指針C語言void

2011-07-01 14:28:47

Qt 指針

2010-01-26 13:42:28

C++指針

2011-04-11 11:09:50

this指針

2021-07-30 05:12:54

智能指針C++編程語言

2010-01-28 13:57:19

C++指針基礎

2021-06-10 08:51:57

C++指針聲明指針相關概念

2014-01-24 09:49:01

C++指針
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 福利精品| 亚洲国产激情 | 精品一区二区三区在线视频 | 国产国产精品 | 中文字幕视频一区二区 | 国产一在线观看 | 欧洲色综合 | 久国久产久精永久网页 | 精品美女在线观看视频在线观看 | av国产精品 | 久久亚洲国产 | 蜜月aⅴ国产精品 | 密乳av| 在线不卡视频 | 欧美性影院 | 午夜久久久| 91视频免费在观看 | 少妇一级淫片免费放播放 | 国产激情视频网站 | 正在播放国产精品 | 国产精品久久一区二区三区 | 久久亚洲欧美日韩精品专区 | 一级特黄a大片 | 狠狠av | 日韩成人| 亚洲国产精品人人爽夜夜爽 | 久久最新精品 | 一区二区三区欧美大片 | 久久精品欧美一区二区三区不卡 | 欧美日韩不卡合集视频 | 日韩电影中文字幕在线观看 | 日韩免费福利视频 | 久久久精品久 | 韩日视频在线观看 | 呦呦在线视频 | 97精品超碰一区二区三区 | 国产精品我不卡 | 黄色在线观看网站 | 中文字幕av亚洲精品一部二部 | 免费毛片网 | 精品在线一区二区 |