Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class IntCounter
- {
- private:
- int *value;
- int *counter;
- public:
- IntCounter(int *value)
- {
- this->value = value;
- counter = new int(1);
- }
- IntCounter(const IntCounter &other)
- {
- value = other.value;
- counter = other.counter;
- (*counter)++;
- }
- ~IntCounter()
- {
- (*counter)--;
- if (*counter == 0 && value)
- {
- delete value;
- }
- }
- int getValue() { return *value; }
- };
- int main()
- {
- int *a = new int(5);
- IntCounter b(a);
- IntCounter c = b;
- IntCounter d = b;
- cout << d.getValue();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement