智能指针auto_ptr使用注意事项

auto_ptr使用入门与注意事项:
1、auto_ptr不能共享所有权。
   An auto_ptr must not refer to an object that is owned by another auto_ptr (or other object).

   下面的程序说明了所有权的问题:
   //util/autoptr1.cpp

   #include <iostream>
   #include <memory>
   using namespace std;


   /* define output operator for auto_ptr
    * - print object value or NULL
    */
   template <class T>
   ostream& operator<< (ostream& strm, const auto_ptr<T>& p)
   {
       //does p own an object ?
       if (p.get() == NULL) {
           strm << "NULL";        //NO: print NULL
        }
        else {
           strm << *p;           //YES: print the object
        }
        return strm;
   }


   int main()
   {
       auto_ptr<int> p(new int(42));
       auto_ptr<int> q;


       cout << "after initialization:" << endl;
       cout << " p: " << p << endl;
       cout << " q: " << q << endl;


       q = p;
       cout << "after assigning auto pointers:" << endl;
       cout << " p: " << p << endl;
       cout << " q: " << q << endl;


       *q += 13;                   //change value of the object q owns
       p = q;
       cout << "after change and reassignment:" << endl;
       cout << " p: " << p << endl;
       cout << " q: " << q << endl;
   }
The output of the program is as follows:

   after initialization:
    p: 42
    q: NULL
   after assigning auto pointers:
    p: NULL
    q: 42
   after change and reassignment:
    p: 55
    q: NULL

2、auto_ptr不能指向数组
3、auto_ptr不能作为容器的成员。
4、不能通过赋值操作来初始化auto_ptr
    std::auto_ptr<int> p(new int(42));     //OK
    std::auto_ptr<int> p = new int(42);    //ERROR
   这是因为auto_ptr 的构造函数被定义为了explicit
5、下面的程序说明了 const auto_ptr的行为:
    const auto_ptr与 type * const ptr;相似
   //util/autoptr2.cpp

   #include <iostream>
   #include <memory>
   using namespace std;


   /* define output operator for auto_ptr
    * - print object value or NULL
    */
   template <class T>
   ostream& operator<< (ostream& strm, const auto_ptr<T>& p)
   {
       //does p own an object ?
       if (p.get() == NULL) {
           strm << "NULL";       //NO: print NULL
       }
       else {
           strm << *p;           //YES: print the object
       }
       return strm;
   }


   int main()
   {
       const auto_ptr<int> p(new int(42));
       const auto_ptr<int> q(new int(0));
       const auto_ptr<int> r;
       cout << "after initialization:" << endl;
       cout << " p: " << p << endl;
       cout << " q: " << q << endl;
       cout << " r: " << r << endl;


       *q = *p;
   //  *r = *p;    //ERROR: undefined behavior
       *p = -77;
       cout << "after assigning values:" << endl;
       cout << " p: " << p << endl;
       cout << " q: " << q << endl;
       cout << " r: " << r << endl;


   //  q = p;      //ERROR at compile time
   //  r = p;      //ERROR at compile time
   }
Here, the output of the program is as follows:

   after initialization:
    p: 42
    q: 0
    r: NULL
   after assigning values:
    p: -77
    q: 42
    r: NULL

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值