Demonstrates the use of auto_ptr. : Auto Pointer « Pointer « C++






Demonstrates the use of auto_ptr.

  
#include <iostream>
#include <memory>
using namespace std;
   
class MyClass {
public:
  MyClass() { 
     cout << "constructing\n"; 
  }
  ~MyClass() { 
     cout << "destructing\n"; 
  }
  void f() { 
     cout << "Inside f()\n"; 
  }
};
   
int main()
{
  auto_ptr<MyClass> myClassPointer1(new MyClass), myClassPointer2;
   
  myClassPointer2 = myClassPointer1; 
  myClassPointer2->f();
   
  MyClass *ptr = myClassPointer2.get();
  ptr->f();
   
  return 0;
}
  
    
  








Related examples in the same category

1.Demonstrate an auto_ptr.Demonstrate an auto_ptr.