zoukankan      html  css  js  c++  java
  • *** auto_ptr指针应用(注意auto_ptr已经被弃用)

    #include <iostream>
    #include <memory>
    #include <string.h>
    
    using namespace std;
    
    class Test
    {
    public:
        Test():name(NULL){}
        Test(const char * x)
        {
            if (x != NULL)
            {
                name = new char [strlen(x)+1];
                strcpy(name, x);
            }
        }
        ~Test()
        {
            if (name != NULL)
            {
                delete [] name;
                name = NULL;
            }
            cout << "Delete name" << endl;
        }
        Test & operator = (const char * str)
        {
            if (name != NULL)
            {
                delete [] name;
            }
            
            name = new char [strlen(str)+1];
            strcpy (name, str);
            
            return *this;
        }
        void ShowName (void)
        {
            cout << name << endl;
        }
    
    public:
        char * name;
    };
    
    int main()
    {
        auto_ptr<Test> TestPtr(new Test("Jason"));
        //Test * TestPtr = new Test("Jason");
        TestPtr->ShowName();
        *TestPtr = "Becky";
        TestPtr->ShowName();
        
        int y = 1;
        int x = 0;
        y = y / x;
        
        cout << "End of program!" << endl;
        
        return 0;
    }

    运行后屏幕输出为:说明出现异常后auto_ptr指向的对象的析构函数仍然被执行了。

    Jason                                                                                                                                                                              
    Becky                                                                                                                                                                              
    End of program!                                                                                                                                                                    
    Delete name

    如果把上面代码中注释掉的代码打开,如下:

    int main()
    {
        //auto_ptr<Test> TestPtr(new Test("Jason"));
        Test * TestPtr = new Test("Jason");
        TestPtr->ShowName();
        *TestPtr = "Becky";
        TestPtr->ShowName();
        
        int y = 1;
        int x = 0;
        y = y / x;
        
        cout << "End of program!" << endl;
        
        return 0;
    }

    执行后屏幕显示如下:因为没有运用auto_ptr而用的裸指针,可以看到析构函数没有被调用

    Jason                                                                                                                                                                              
    Becky                                                                                                                                                                              
    End of program!
  • 相关阅读:
    TP
    vim manual 个人笔记
    关于动画属性
    过渡
    关于 css3 的filter属性
    html 中行内元素和块级元素区别
    JS以不同的格式保存文件内容
    64位Kali无法顺利执行pwn1问题的解决方案
    鱼龙混杂 · 数据结构学习笔记(01)
    Terminal(终端) 在 OS X下如何快速调用
  • 原文地址:https://www.cnblogs.com/superrunner/p/10225757.html
Copyright © 2011-2022 走看看