C++ 的 auto_ptr 所做的事情, 就是动态分配对象以及当对象不再需要时自动执行清理, 该智能指针在 C++11 中已经被弃用, 转而由 unique_ptr 替代,
那这次使用和实现, 就具体讲一下 auto_ptr 被弃用的原因,(编译平台: Linux centos 7.0 编译器: gcc 4.8.5)
首先使用 std::auto_ptr 时, 需要 #include <memory > 头文件, 具体使用代码如下 (文件名: test_ptr.cpp):
- #include <memory>
- #include <iostream>
- using namespace std;
- class Test
- {
- public:
- Test()
- {
- cout <<"construct.." << endl;
- }
- ~Test()
- {
- cout << "destruct.." << endl;
- }
- };
- void test()
- {
- }
- int main()
- {
- Test* p = new Test();
- auto_ptr<Test> ap(p);
- return 0;
- }
执行上述代码, 我们可以看到, 程序结束时, 在堆上申请的对象, 自动执行了析构函数, 将内存释放了
- [root@localhost code]# g++ -g -o autop test_ptr.cpp
- [root@localhost code]# ./autop
construct..
destruct..
[root@localhost code]#
具体实现代码如下, 构造函数只实现了初始化构造和拷贝构造:
- #include <iostream>
- using namespace std;
- template<typename T>
- class auto_pt
- {
- public:
- explicit auto_pt(T* p = NULL):m_ptr(p)
- {
- p = NULL;
- cout <<"auto_ptr construct" << endl;
- }
- auto_pt(auto_pt& autoPtr):m_ptr(autoPtr.m_ptr)
- {
- autoPtr.m_ptr = NULL;
- cout << "copy auto_ptr construct" << endl;
- }
- auto_pt& operator = (auto_pt& p)
- {
- if(this != &p)
- {
- if(m_ptr != NULL)
- {
- delete m_ptr;
- m_ptr = p.m_ptr;
- p.m_ptr = NULL;
- }
- }
- return *this;
- }
- ~auto_pt()
- {
- if(m_ptr != NULL)
- {
- cout << "auto_ptr destruct" << endl;
- delete m_ptr;
- m_ptr = NULL;
- }
- }
- T* Get()
- {
- return m_ptr;
- }
- T& operator*()
- {
- return *m_ptr;
- }
- T* operator->()
- {
- return m_ptr;
- }
- private:
- T* m_ptr;
- };
- class Test
- {
- public:
- Test()
- {
- cout <<"construct.." << endl;
- }
- ~Test()
- {
- cout << "destruct.." << endl;
- }
- void method()
- {
- cout << "welcome Test.." << endl;
- }
- };
- void f(auto_pt<Test>ap)
- {
- cout <<"funtion f :";
- ap->method();
- }
- int main()
- {
- //baseic test
- Test* p = new Test();
- cout <<"address0 [%p]" << p << endl;
- auto_pt<Test> ap(p);
- cout <<"address1 [%p]" << ap.Get()<< endl;
- cout << "address2 [%p]" << &ap << endl;
- cout << "address3 [%p]" << &(*ap) << endl;
- ap.Get()->method();
- (*ap).method();
- ap->method();
- return 0;
- }
打印结果:
- [root@localhost code]# g++ -o autop_test test.cpp
- [root@localhost code]# ./autop_test
construct..
- address0 [%p]0xb77010
- auto_ptr construct
- address1 [%p]0xb77010
address2 [%p]0x7ffe8b25f510
address3 [%p]0xb77010
welcome Test..
welcome Test..
welcome Test..
auto_ptr destruct
destruct..
[root@localhost code]#
大概实现就是这样, 基本和标准库差不多, 除了另外两种类型的构造函数没有加进去
那在我们使用及实现的过程中, 发现这个 auto_ptr 在使用过程中会有如下风险, 因此在 C++11 中已经不再使用, 那在我们开发过程中, 也最好不要再使用
1. 两个 auto_ptr 指向同一块内存, 造成多次释放
- //if 2 object point one address, application will die
- Test* p1 = new Test();
- auto_pt<Test> ap1(p1);
- auto_pt<Test> ap2(p1);
2. 复制完成后, 会将复制的对象置空, 因此不能继续使用
- int*p=new int();
- auto_pt<int>ap1(p);
- auto_pt<int>ap2=ap1;
- (*ap1).method();// 错误, 此时 ap1 只剩一个 null 指针在手了
3. 函数形参使用值传递, 会发生拷贝操作, 导致 ap1 对象权限获取不到了
- void f(auto_pt<int>ap)
- {
- (*ap).method();
- }
- auto_pt<int>ap1(new int(0));
- f(ap1);
- (*ap1).method();;// 错误, 经过 f(ap1) 函数调用, ap1 已经不再拥有任何对象了.
来源: https://www.cnblogs.com/blog-yejy/p/8910071.html