fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include<iostream>
  5. using namespace std;
  6.  
  7. class SmartPtr
  8. {
  9. private:
  10. int *ptr; // Actual pointer
  11. public:
  12. explicit SmartPtr(int *p = NULL) { ptr = p; }
  13.  
  14. // Destructor
  15. ~SmartPtr() { delete(ptr); }
  16.  
  17. // Overloading dereferencing operator
  18. int & operator *() { return *ptr; }
  19. };
  20.  
  21. int main()
  22. {
  23. SmartPtr ptr(new int());
  24. *ptr=2016;
  25. cout << *ptr << endl;
  26.  
  27.  
  28. // We don't need to call delete ptr: when the object
  29. // ptr goes out of scope, destructor for it is automatically
  30. // called and destructor does delete ptr.
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0s 4188KB
stdin
Standard input is empty
stdout
2016