fork download
  1. #include <iostream>
  2.  
  3.  
  4. template<class T>
  5. class Auto_ptr{
  6. public:
  7. // constructors
  8. explicit Auto_ptr(T* p = NULL): value(p) { }; // constructor
  9. Auto_ptr(Auto_ptr& p); // copy constructor
  10. Auto_ptr& operator= (const Auto_ptr& p); // copy assignment
  11. ~Auto_ptr() { std::cout << "pointer deleted\n"; delete value; } // destructor
  12.  
  13. // access operators
  14. const T& operator* () const { return *value; } // dereference operator
  15. const T* operator->() const { return value; } // indirect class member access (arrow) operator
  16.  
  17. // non-modifying members
  18. T* get() { return value; } // getter method
  19. void reset(T* v); // reassing new value(default value: nullptr)
  20. T* release(); // transfers the object to another pointer; without destroying it
  21. private:
  22. // data member
  23. T* value;
  24.  
  25. };
  26.  
  27. //--------------------------------------------------------------------------------------------------------
  28. // class Auto_ptr member implementations
  29. // Constructors
  30. // copy constructor
  31. template<class T>
  32. Auto_ptr<T>::Auto_ptr(Auto_ptr& p) {
  33. value = p.release();
  34. }
  35.  
  36. // copy assignment
  37. template<class T>
  38. Auto_ptr<T>& Auto_ptr<T>::operator= (const Auto_ptr& p ) {
  39. if (this == &p) return *this;
  40. if (value) delete value;
  41. value = p.value;
  42. return *this;
  43. }
  44.  
  45. /*
  46. Function: release()
  47. Use: T ptr = auto_ptr_obj.release();
  48.  
  49. It transfers the pointer value to the
  50. caller, setting the data member value
  51. to nullptr.
  52. */
  53. template <class T>
  54. T* Auto_ptr<T>::release() {
  55. T* temp = value;
  56. value = NULL;
  57. return temp;
  58. }
  59.  
  60. /*
  61. Function: reset()
  62. Use: auto_ptr_obj.release(new_pointer);
  63.  
  64. It deletes the object pointer to by
  65. pointer value and assings new_pointer;
  66. */
  67. template <class T>
  68. void Auto_ptr<T>::reset(T* v) {
  69. delete value;
  70. value = v;
  71. }
  72.  
  73.  
  74. void test1 () {
  75. std::cout <<"\nTest constructor and get() member.\n";
  76. Auto_ptr<int> p(new int);
  77. *p.get() = 5;
  78. std::cout <<"p points to: "<< *p << "\n";
  79.  
  80. //assert(*p, 5);
  81. std::cout <<"TEST 1 DONE\n";
  82. }
  83.  
  84. void test2 () {
  85. std::cout <<"\nTest reset() and release() members.\n";
  86. Auto_ptr<int> p(new int);
  87. *p.get() = 5;
  88. std::cout <<"p points to: "<< *p << "\n";
  89.  
  90. p.reset(new int(10));
  91. std::cout <<"reset() p points to: "<< *p << "\n";
  92. //assert(*p, 10);
  93.  
  94. int *temp = p.release();
  95. std::cout <<"caller of release(), temp points to: "<< *temp << "\n";
  96. //assert(*temp, 10);
  97.  
  98. // nullptr dereferece error
  99. // std::cout <<"p after being release()d points to: "<< *p << "\n";
  100.  
  101. std::cout <<"TEST 2 DONE\n";
  102. }
  103.  
  104. void test3 () {
  105. std::cout <<"\nTest copy constructor and copy assignment.\n";
  106. Auto_ptr<int> p1(new int(10));
  107. Auto_ptr<int> p2(p1);
  108.  
  109. std::cout <<"copy constructed p2 points to: "<< *p2 << "\n";
  110. //assert(*p2, 10);
  111.  
  112. Auto_ptr<int> p3(new int(20));
  113. p1 = p3;
  114. std::cout <<"copy assigned p1 points to: "<< *p1 << "\n";
  115. //assert(*p1, 20);
  116.  
  117. std::cout <<"TEST 3 DONE\n";
  118. }
  119.  
  120.  
  121. int main () {
  122. test1 ();
  123. test2 ();
  124. test3 ();
  125.  
  126.  
  127. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Test constructor and get() member.
p points to: 5
TEST 1 DONE
pointer deleted

Test reset() and release() members.
p points to: 5
reset() p points to: 10
caller of release(), temp points to: 10
TEST 2 DONE
pointer deleted

Test copy constructor and copy assignment.
copy constructed p2 points to: 10
copy assigned p1 points to: 20
TEST 3 DONE
pointer deleted
pointer deleted
pointer deleted