fork download
  1. // if (!sPtr2.isAssigned ()) // SmartPtr::isAssigned() returns false if no reference is being held
  2. // cout << "sPtr2 is not holding any reference" << endl;
  3. // sPtr2 = new T; // make a new oject of T and pass ownership to sPtr
  4. //
  5. #include <algorithm>
  6.  
  7. //#define NULL nullptr
  8. template<class T>
  9. class SmartPtr
  10. {
  11. public:
  12.  
  13. // create a new object which is not
  14. // refering to any object on heap.
  15. // no-throw guarantee
  16. explicit SmartPtr ()
  17. : m_pT (NULL),
  18. m_pRefCount (NULL)
  19. {
  20. }
  21.  
  22. // new object will point to a memory location on heap given by 'pObj'
  23. // Strong exception guarantee
  24. explicit SmartPtr (T *pObj)
  25. : m_pT (pObj),
  26. m_pRefCount (NULL)
  27. {
  28. try
  29. {
  30. m_pRefCount = new int;
  31. }
  32. catch (...)
  33. {
  34. checkedDelete (m_pT);
  35. throw;
  36. }
  37.  
  38. *m_pRefCount = 1;
  39. }
  40.  
  41. // new object will refer to same memory on heap as 'rObj'
  42. // no-throw guarantee
  43. SmartPtr (const SmartPtr<T> &rObj)
  44. : m_pT(rObj.m_pT),
  45. m_pRefCount(rObj.m_pRefCount)
  46. {
  47. if (m_pRefCount != NULL)
  48. (*m_pRefCount)++;
  49. }
  50.  
  51.  
  52. // make 'rObj' and 'this' will refer to same object on heap
  53. // no-throw guarantee
  54. SmartPtr& operator= (const SmartPtr<T> &rObj)
  55. {
  56. // uses copy-and-swap idiom
  57. this_type(rObj).swap(*this);
  58. return *this;
  59. }
  60.  
  61. // assign this smart pointer to another object on heap
  62. // Strong exception guarantee
  63. SmartPtr& operator= (T *pTObj)
  64. {
  65. // try and setup memory for reference counter
  66. int *pNewRefCount;
  67. try
  68. {
  69. pNewRefCount = new int;
  70. }
  71. catch (...)
  72. {
  73. delete pTObj;
  74. throw;
  75. }
  76.  
  77. // stop referring to previous object
  78. updateCountAndTriggerDelete ();
  79.  
  80. // start referring to new object
  81. m_pRefCount = pNewRefCount;
  82. *m_pRefCount = 1;
  83.  
  84. m_pT = pTObj;
  85.  
  86. return *this;
  87. }
  88.  
  89. // no-throw guarantee
  90. ~SmartPtr ()
  91. {
  92. updateCountAndTriggerDelete ();
  93. }
  94.  
  95. // returns true if this object is holding a reference
  96. // to an object on heap
  97. // no-throw guarantee
  98. bool isAssigned()
  99. {
  100. return !(m_pT == NULL);
  101. }
  102.  
  103. // no-throw guarantee
  104. T* operator->()
  105. {
  106. return m_pT;
  107. }
  108.  
  109. // no-throw guarantee
  110. T& operator*()
  111. {
  112. return *m_pT;
  113. }
  114.  
  115. private:
  116.  
  117. // make sure we dont delete a incomplete type pointer
  118. // no-throw guarantee
  119. template <class S>
  120. void checkedDelete (S* pSObj)
  121. {
  122. typedef char type_must_be_complete[ sizeof(S)? 1: -1 ];
  123. (void) sizeof(type_must_be_complete);
  124. delete pSObj;
  125. }
  126.  
  127. // count the references and delete it if this is last reference
  128. // no-throw guarantee
  129. void updateCountAndTriggerDelete ()
  130. {
  131. if (m_pRefCount != NULL)
  132. {
  133. (*m_pRefCount)--;
  134.  
  135. // if this is last reference delete the memory
  136. if (*m_pRefCount == 0)
  137. {
  138. checkedDelete (m_pRefCount);
  139. checkedDelete (m_pT);
  140. }
  141. }
  142. }
  143.  
  144. // swap the pointer values of 'rObj' with values of 'this'
  145. // no-throw guarantee
  146. void swap (SmartPtr<T> &rObj)
  147. {
  148. std::swap (m_pT, rObj.m_pT);
  149. std::swap (m_pRefCount, rObj.m_pRefCount);
  150. }
  151.  
  152. // pointer to memory location of object
  153. T *m_pT;
  154.  
  155. // pointer to memory location where 'reference' count of
  156. // object pointed to by m_pT is kept
  157. int *m_pRefCount;
  158.  
  159. typedef SmartPtr<T> this_type;
  160. };
  161.  
  162.  
  163. int main()
  164. {
  165. int *oldPtr = new int();
  166. SmartPtr<int> sPtr= oldPtr;
  167. SmartPtr<int> sPtr2;
  168. sPtr2= oldPtr;
  169.  
  170. }
  171.  
Compilation error #stdin compilation error #stdout 0s 3024KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:166:25: error: conversion from ‘int*’ to non-scalar type ‘SmartPtr<int>’ requested
stdout
Standard output is empty