zoukankan      html  css  js  c++  java
  • share_ptr指向的对象的析构动作在创建的时候被捕获

    share_ptr智能指针能够记录其实际的指向对象的类型,并正确的析构该对象。
    所以使用share_ptr来管理的对象不需要虚析构函数

    示例代码

    #include <iostream>
    #include <memory>
    using namespace std;
    
    class base{
    public:
            ~base(){cout << "in ~base()" << endl;}
    };
    
    class derive : public base
    {
    public:
            ~derive(){cout << "In ~derive()" << endl;}
    
    };
    
    
    int main()
    {
            base* p =new derive;
            delete p;
            cout << "=============================" <<endl;
            {
                    shared_ptr<base> ptr(new derive);
            }
            while(1);
            return 0;
    }
    

    运行结果

    [lhx@lhx-master share_ptr]$ ./a.out 
    in ~base()
    =============================
    In ~derive()
    in ~base()
    
    
  • 相关阅读:
    实验三 进程调度模拟程序
    实验二作业调度模拟程序实验报告
    实验8
    实验七
    实验六
    实验五 数独游戏界面设置
    实验五
    实验四
    实验三
    实验二
  • 原文地址:https://www.cnblogs.com/DXGG-Bond/p/14684318.html
Copyright © 2011-2022 走看看