zoukankan      html  css  js  c++  java
  • auto_ptr的不足

    一开始还发现这货挺方便的.后来才发现还是不行,

    多引用时,就会重复删除了

    #include <iostream>
    #include <memory>
    using namespace std;
    
    
    void func(const auto_ptr<int>& pInt)
    {
        cout << *pInt<<endl;
    }
    
    class X
    {
    public:
        X(){cout <<"construct"<<endl;}
        ~ X(){cout <<"destructor"<<endl;}
    };
    
    int main()
    {
    #if 0
        auto_ptr<int> a(new int(100));
        func(a);
    #endif
    
    
        //error use begin
         X* x = new X;
        auto_ptr<X> p1(x);
    
        delete x;        //destructor 1
    
        //error use begin
        return 0;
    }
        //destructor 2
    
    #if 0  //normal use
    
    
    int main()
    {
        cout <<"before scope"<<endl;
        {
        auto_ptr<X> p1(new X);
        cout <<"I will exit the scope" <<endl;
    
        }
        cout <<"after scope"<<endl;
    
        cout <<"------------------------------"<<endl;
        cout <<"before scope"<<endl;
        {
        auto_ptr<X> p1(new X);
        delete p1;;
        cout <<"I will exit the scope" <<endl;
    
        }
        cout <<"after scope"<<endl;
        return 0;
    }
    #endif //normal use
  • 相关阅读:
    jquery $.ajax $.get $.post的区别
    浅析JQuery中的html(),text(),val()区别
    单词统计续
    第一阶段意见评论
    学习进度9
    第一阶段SCRUM冲刺10
    第一阶段SCRUM冲刺09
    单词统计
    第一阶段SCRUM冲刺08
    学习进度8
  • 原文地址:https://www.cnblogs.com/vimmer/p/2979956.html
Copyright © 2011-2022 走看看