Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 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 = nullptr)
  13.     {
  14.         if (value)
  15.         {
  16.             this->value = value;
  17.             counter = new int(1);
  18.         }
  19.     }
  20.  
  21.     IntCounter(const IntCounter &other)
  22.     {
  23.         value = other.value;
  24.         counter = other.counter;
  25.         (*counter)++;
  26.     }
  27.  
  28.     ~IntCounter()
  29.     {
  30.         (*counter)--;
  31.         if (*counter == 0 && value)
  32.         {
  33.             delete value;
  34.         }
  35.     }
  36.  
  37.     int getValue() { return *value; }
  38. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement