C++98智能指针auto

it2023-09-02  63

大家不是一直吐槽C/C++的指针都要程序员自己管理嘛,不用担心啦 智能指针就可以解决这样的问题【说是智能其实就是一个模板类,声明周期结束析构释放】。

auto_ptr 是c++ 98定义的智能指针模板,其定义了管理指针的对象,可以将new 获得(直接或间接)的地址赋给这种对象。当对象过期时,其析构函数将使用delete 来释放内存!【后面介绍C++11新增的智能指针】

头文件:    #include <memory>

用  法:    auto_ptr<类型> 变量名(new 类型)

#include <memory> #include <iostream> #include <string> #include <vector> #include <algorithm> #include <exception> #include <Windows.h> using namespace std; class Temp { public: Temp() { cout << "构造函数" << endl; debug = 0; } Temp(int val) { cout << "构造函数" << endl; debug = val; } ~Temp() { cout << "析构函数" << endl; } int getDebug()const { return debug; } private: int debug; }; void au
最新回复(0)