Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class IntCounter
  6. {
  7. private:
  8.     int *value;
  9.     int *counter;
  10.  
  11. public:
  12.     IntCounter(int *value)
  13.     {
  14.         this->value = value;
  15.         counter = new int(1);
  16.     }
  17.  
  18.     IntCounter(const IntCounter &other)
  19.     {
  20.         value = other.value;
  21.         counter = other.counter;
  22.         (*counter)++;
  23.     }
  24.  
  25.     ~IntCounter()
  26.     {
  27.         (*counter)--;
  28.         if (*counter == 0 && value)
  29.         {
  30.             delete value;
  31.         }
  32.     }
  33.  
  34.     int getValue() { return *value; }
  35. };
  36.  
  37. int main()
  38. {
  39.     int *a = new int(5);
  40.     IntCounter b(a);
  41.     IntCounter c = b;
  42.     IntCounter d = b;
  43.  
  44.     cout << d.getValue();
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement