fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4.  
  5. class RefCounter
  6. {
  7. private:
  8. int count = 0;
  9.  
  10. public:
  11. void AddRef()
  12. {
  13.  
  14. count++;
  15. }
  16.  
  17. int Release()
  18. {
  19. return --count;
  20. }
  21. int getCount()
  22. {
  23.  
  24. return count;
  25. }
  26. };
  27.  
  28. template<typename T> class sharedPTR
  29. {
  30. private:
  31. T* myData;
  32. RefCounter* myRef;
  33.  
  34. public:
  35. sharedPTR() :
  36. myData(0), myRef(0)
  37. {
  38.  
  39. myRef = new RefCounter();
  40. myRef->AddRef();
  41. }
  42.  
  43. sharedPTR(T* pValue) :
  44. myData(pValue), myRef(0)
  45. {
  46.  
  47. myRef = new RefCounter();
  48. myRef->AddRef();
  49.  
  50. }
  51.  
  52. ~sharedPTR()
  53. {
  54. if (myRef && myRef->Release() == 0)
  55. {
  56. delete myData;
  57. delete myRef;
  58. }
  59. }
  60.  
  61. sharedPTR(const sharedPTR<T>& ref) : myData(nullptr), myRef(nullptr)
  62. {
  63. if (ref.myRef && ref.myRef->getCount() < 3) {
  64. myData = ref.myData;
  65. myRef = ref.myRef;
  66. myRef->AddRef();
  67. }
  68. }
  69.  
  70. sharedPTR<T>& operator = (const sharedPTR<T>& ref)
  71. {
  72. if (this == &ref) {
  73. return *this;
  74. }
  75. if (myRef && myRef->Release() == 0)
  76. {
  77. delete myData;
  78. delete myRef;
  79. }
  80. myData = nullptr;
  81. myRef = nullptr;
  82. if (ref.myRef && ref.myRef->getCount() < 3) {
  83. myData = ref.myData;
  84. myRef = ref.myRef;
  85. myRef->AddRef();
  86. }
  87. return *this;
  88. }
  89.  
  90. T& getData()
  91. {
  92. return *myData;
  93. }
  94.  
  95. T* getPointer()
  96. {
  97. return myData;
  98. }
  99. };
  100.  
  101. class Cat
  102. {
  103. public:
  104. Cat(unsigned int w_score, const std::string& w_name) :
  105. score(w_score),
  106. name(w_name)
  107. {}
  108. std::string getName()
  109. {
  110. return name;
  111. }
  112.  
  113. unsigned int score;
  114. private:
  115. std::string name;
  116. };
  117.  
  118. int main(void)
  119. {
  120. sharedPTR<Cat> newCat(new Cat(0, "Ferrari"));
  121.  
  122. // should print "Ferrari"
  123. std::cout << newCat.getPointer()->getName() << std::endl;
  124.  
  125. // should also print "Ferrari"
  126. std::cout << newCat.getData().getName() << std::endl;
  127.  
  128. newCat.getData().score = 50;
  129.  
  130. // should printf "50", as it was just assigned before
  131. std::cout << newCat.getPointer()->score << std::endl;
  132.  
  133. // make some copies
  134. sharedPTR<Cat> copyOfnewCat = newCat;
  135. sharedPTR<Cat> copyOfnewCat2 = newCat;
  136.  
  137. // this copy should fail
  138. sharedPTR<Cat> copyOfnewCat3 = newCat;
  139.  
  140. // should be nullptr
  141. std::cout << copyOfnewCat3.getPointer() << std::endl;
  142.  
  143. // should be something other than 0 and equal
  144. std::cout << "copy2 pointer: " << copyOfnewCat2.getPointer() << " copy1 pointer: " << copyOfnewCat.getPointer() << std::endl;
  145. copyOfnewCat2.getData().score = 40;
  146.  
  147. // should be 40 now
  148. std::cout << newCat.getPointer()->score << std::endl;
  149. sharedPTR<Cat> copyOfnewCat4(newCat);
  150.  
  151. // should be nullptr
  152. std::cout << copyOfnewCat4.getPointer() << std::endl;
  153. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
Ferrari
Ferrari
50
0
copy2 pointer: 0x2af4ede9dc20 copy1 pointer: 0x2af4ede9dc20
40
0