使用auto_ptr需要注意的事项

a. auto_ptr定义于头文件memory中;

b. auto_ptr只能用来管理单个动态创建的对象,而不能管理动态创建的数组;

c. 和其他copy和assign不同,auto_ptr的copy和assign会改变右边的操作数,assignment符号的两边的auto_ptr均为左值;There is a crucially important difference between how auto_ptr and built-in pointers treat copy and assignment. When we

copy an auto_ptr or assign its value to another auto_ptr, ownership of the underlying object is transferred from the original

to the copy. The original auto_ptr is reset to an unbound state;

d. auto_ptr不能作为容器中的元素;

auto_ptr的copy和assign具有析构行为,这就是auto_ptr不能作为容器元素的原因,因为标准库中的容器有对元素的要求:经

过copy或者assign后的两个对象,必须相等;

e. 在判断一个auto_ptr是否被绑定的时候,不能直接使用auto_ptr对象:

      auto_ptr<Student> stu1(new Student);

         if(stu1)

         {

                   cout << "stu1 is bound" << endl;

         }

         else

         {

                   cout << "stu1 is unbound" << endl;

         }

         这样做将会导致compile error,应该改为:

         auto_ptr<Student> stu1(new Student);

         if(stu1.get())               // get()获取的是underlying对象的指针,如果被绑定则非零,如果没有被绑定则为0

         {

                   cout << "stu1 is bound" << endl;

         }

         else

         {

                   cout << "stu1 is unbound" << endl;

         }

f. auto_ptr的构造函数是explicit的,消除了隐式的类型转换(在这里即,从指针类型到auto_ptr类型的转换),因此不能直接将一个

指针赋给一个auto_ptr对象。如下面这样的代码:

         auto_ptr<Student> stu5 = new Student;

         stu5->printStudentInfo();

在编译的时候不会有问题,但会出现严重的runtime error。正确的做法应该是:

         auto_ptr<Student> stu5(new Student);

         stu5->printStudentInfo();

g. 不同用两个auto_ptr绑定到同一个对象。

      // stu6和stu7绑定到了同一个对象,这将会导致该对象被析构两次,将会产生runtime error

         auto_ptr<Student> stu6(new Student("Evanligine", "F", 8));

         auto_ptr<Student> stu7(stu6.get());

         后面一句,应该改为:

         auto_ptr<Student> stu7(stu6);

         这样stu6就将ownership转交给了stu7,stu6则成为了unbound的auto_ptr。

h. 不能用auto_ptr指向静态资源分配对象。如下面的代码,尽管可以通过编译,但将会产生runtime error:

         int ix = 10;

         auto_ptr<int> pint1(&ix);

i. auto_ptr的重要操作

auto_ptr<T> ap;                 创建一个未绑定的auto_ptr对象ap

auto_ptr<T> ap(p);          创建一个auto_ptr对象ap,它绑定了指针p所指向的对象。该构造函数是explicit的

auto_ptr<T> ap1(ap2);   创建一个auto_ptr对象ap1,它绑定到原来被ap2绑定的对象,ap2则成为为绑定的auto_ptr

ap1 = ap2;                            ap1删除原来绑定的对象,ap2将ownership移交给ap1,ap2成为未绑定的auto_ptr

*ap                                        返回ap绑定的对象的引用。可以通过*给被绑定的内在对象赋值。如下面代码:

                                                        auto_ptr<int> pint(new int(3));

                        cout << *pint << endl;          // 输出3

                        *pint = 100;

                        cout << *pint << endl;          // 输出100

ap->                                      返回被ap绑定的对象的指针

ap.reset(p)                          如果指针p和ap绑定的内存对象的指针不相同,那么ap删除被其绑定的内存对象,改而绑定p所

指向的对象.

ap.release()                         返回ap所绑定对象的指针,并删除该被绑定的对象

ap.get()                           返回ap所绑定对象的指针

---------------------------

转自 玄机逸士的专栏


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值