#include #include // http://www.kennyandkarin.com/kenny/AutoPtr.h // http://advancedstocktracker.org/doxygen/autoptr_8h-source.html // http://www.koders.com/cpp/fid1E910DB99E5060114A9C69C23F3FBAD3AE6735D5.aspx?s=mdef%3Ainsert // #include // #pragma once // #define auto_ptr AutoPtr // #include "countedptr.h" #include "autoptr.h" using namespace std; class A { public: virtual void tellme() { cout << "I'am class A: " << m_name << endl; } A(string _name) : m_name(_name) {} virtual ~A() { cout << "Vow ! :" << m_name << " deleted" << endl; } private: std::string m_name; }; AutoPtr ptr2; void func1(AutoPtr a, std::string s) { a->tellme(); AutoPtr ptr3(new A(string("ptr") + s)); ptr3->tellme(); ptr2 = ptr3; } void func1(A &a) { a.tellme(); } int main() { AutoPtr p(new A("moma")); AutoPtr p2; /* for (int i =0; i< 10000; i++) { int i=1; stringstream ss; ss << i+1; func1(p, ss.str()); } // p->tellme(); ptr2->tellme(); // p2.explicitly_delete(); // ptr2.explicitly_delete(); */ func1(p, "1"); ptr2->tellme(); p2 = p; cout << "ref count=" << p.getRefCount() << endl; if (p2 == p) { cout << "p and p2 are the same object" << endl; } // A *pp; // pp = ptr2->getPtr(); // p2 = p; // p->tellme(); return 0; }