码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 智能指针shared_from_this


    shared_from_this()的用途

    资源对象的成员方法(不包括构造函数和析构函数)需要获取指向对象自身,即包含了this指针的shared_ptr

    使用原因

    1.把当前类对象作为参数传给其他函数时,为什么要传递share_ptr呢?直接传递this指针不可以吗?

    一个裸指针传递给调用者,谁也不知道调用者会干什么?假如调用者delete了该对象,而share_tr此时还指向该对象。

    2.这样传递share_ptr可以吗?share_ptr

    这样会造成2个非共享的share_ptr指向一个对象,最后造成2次析构该对象。

    使用示例

    #include
    #include
    using namespace std;
    class Base : public std::enable_shared_from_this<Base>
    {
    private:
        /* data */
    public:
        Base(/* args */){ cout << "constructor.."<< endl;}
        ~Base(){ cout << "delete.."<< endl; }
        std::shared_ptr<Base> getSharedFromThis(){return shared_from_this();}
        //注意获取共享指针的函数不能在析构函数或者析构的回调函数中调用
        void fun(){
            std::shared_ptr<Base> basePtr = enable_shared_from_this::shared_from_this();
            cout<< "use_count:"<< basePtr.use_count() <<endl;   //应用计数+2 说明获取共享指针成功
    
        }
    };
    
    class none_donging{
    public:
        void operator()(void* val){
            Base *base = (Base *)val;
            if (base)
            {
                delete base;
            }
        }
    };
    
    typedef std::shared_ptr<Base> BasePtr;
    
    int main(){
        {
            BasePtr basePtr = BasePtr(new Base,[=](void* val){
                //自定义析构
                Base* base = (Base*)val;
                if(base){
                    // base->fun(); //禁止在析构的回调函数中调用
                    delete base;
                }
            });
            basePtr->fun();
        }
        cout <<"==================="<<endl;
        {
            BasePtr basePtr = BasePtr(new Base,none_donging());
            BasePtr basePtr3 = basePtr->getSharedFromThis();
            cout<< "use_count:"<< basePtr3.use_count() <<endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    输出:

    constructor..
    use_count:2
    delete..
    ===================
    constructor..
    use_count:2
    delete..
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    2000-2020年上市公司制造业数据/制造业上市公司数据
    运筹说 第82期 | 算法介绍之图与网络分析(二)
    Node.js 实战 第2章 Node 编程基础 2.9 异步开发的难题
    使用富斯i6遥控器设置6种飞行模式
    ASEMI整流桥堆GBJ406的作用,GBJ406整流桥型号及参数
    mysql—面试50题—1
    uniapp实战项目 (仿知识星球App) - - tabBar配置
    网络安全(黑客)自学
    如何反编译去掉安卓应用的版本更新功能
    智能汽车-ICALL、BCALL、ECALL都是啥
  • 原文地址:https://blog.csdn.net/qq_44519484/article/details/126583412
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号