Poco AutoPtr 例子

tt.cc

#include <cstdlib>
#include <iostream>
#include "Poco/AutoPtr.h"
#include "Poco/RefCountedObject.h"
using Poco::AutoPtr;
using Poco::RefCountedObject;

class A: public RefCountedObject {
   public:
      ~A()
      {
         std::cout << "A died." << std::endl;
      }
};

class C {
   public:
      C(AutoPtr<A> a):
         _a(a)
      {
      }
      ~C()
      {
         std::cout << "C died." << std::endl;
      }
   private:
      AutoPtr<A> _a;
};

// ===  FUNCTION  ======================================================================
//         Name:  main
//  Description:  main function
// =====================================================================================
   int
main ( int argc, char *argv[] )
{
   AutoPtr<A> apa = new A;
   C c(apa);
   apa = NULL;
   return EXIT_SUCCESS;
}     // ----------  end of function main  ----------
[root@slayer autoptr]# g++ tt.cc -lPocoFoundation
[root@slayer autoptr]# ./a.out
C died.
A died.

ttt.cc

这样用没效果

#include <cstdlib>
#include <iostream>
#include "Poco/AutoPtr.h"
#include "Poco/RefCountedObject.h"
using Poco::AutoPtr;
using Poco::RefCountedObject;

class A: public RefCountedObject {
   public:
      ~A()
      {
         std::cout << "A died." << std::endl;
      }
};

class C {
   public:
      C(A &a):
         _a(a)
      {
      }
      ~C()
      {
         std::cout << "C died." << std::endl;
      }
   private:
      A &_a;
};

// ===  FUNCTION  ======================================================================
//         Name:  main
//  Description:  main function
// =====================================================================================
   int
main ( int argc, char *argv[] )
{
   AutoPtr<A> apa = new A;
   C c(*apa);
   apa = NULL;
   return EXIT_SUCCESS;
}     // ----------  end of function main  ----------
[root@slayer autoptr]# g++ ttt.cc -lPocoFoundation
[root@slayer autoptr]# ./a.out
A died.
C died.

 

posted @ 2013-03-15 17:30  Leo Forest  阅读(652)  评论(0编辑  收藏  举报