ReferenceCounter c++

I have class SmartPointer, Date and ReferenceCounter. I have task: To be able to check the value of the counter inside the smart pointer count, add a method useCount()that returns the counter count.
So, this my code, where can i add useCount?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//ReferenceCounter

class ReferenceCounter {
private:
    int count = 0;
public:
    int operator++()
    {
        return count++;
    }
    int operator--()
    {
      return count--;
    }
};

//SmartPointer


template<class T>
class SmartPointer {
private:
    T* x;
    ReferenceCounter* ref;
public:
   SmartPointer();
    explicit SmartPointer(T *X, ReferenceCounter* ref);
    SmartPointer(const SmartPointer<T>& sp);

    ~SmartPointer();

    T &operator*() {
        return *x;
    }

    T *operator->() {
        return x;
    }
    Date useCount() {
//
    }

    SmartPointer<T>& operator == (const SmartPointer<T>& sp){
        if(this != &sp){
            if(ref->operator--() == 0){
                delete x;
                delete ref;
            }
            x = sp.x;
            ref = sp.ref;
            ref->operator++();
        }
        return *this;
    }

    explicit SmartPointer(Date *pDate);
};
template<class T>
SmartPointer<T>::SmartPointer() {
    ref = new ReferenceCounter;
    ref->operator++();
}

template<class T>
SmartPointer<T>::SmartPointer(T *X, ReferenceCounter* ref) {
    this->x = X;
    this->ref = new ReferenceCounter();
    ref->operator++();
}

template<class T>
SmartPointer<T>::SmartPointer(const SmartPointer<T> &sp) {
x = sp.x;
ref = sp.ref;
ref->operator++();
}

template<class T>
SmartPointer<T>::~SmartPointer() {
    if(ref->operator--() == 0){
        delete x;
        delete ref;
    }
}

template<class T>
SmartPointer<T>::SmartPointer(Date *pDate) {
this->x = x;
this->ref = ref;
}
Last edited on
return count++;

Should be replaced with:

return ++count;

useCount() Should be put into smart pointer as a separate variable and getter function, this will tell how many smart pointer object own the resource.

However since your smart pointer acts as wrapper around invasive reference count, you should also have GetRefCount() in ReferenceCounter class, because your design implies derived classes from ReferenceCounter don't need smart pointer.

therefore useCount != RefCount

EDIT:
Unless you don't plan to derive from ReferenceCounter? In which case ReferenceCounter would better be placed into smart pointer as nested class.
Last edited on
Yes, i fix that with deriving but will this work?
template<class T>
int SmartPointer<T>::getCount() const {
ref->operator++();
return count;
}
No it wont work, getCount() job is to return ownership count not to increment or decrement owned object.

You need to make SmartPointer a friend to referenceCounter to avoid writing 2 getcount() functions, that's why it would be the best to have it as private nested class instead and make count variable public.

There are many designs for smart pointer and writing one isn't easy to do, you may want to read the basics first:
https://mortoray.com/2012/01/08/what-is-reference-counting/
Last edited on
class ReferenceCount adds no value over int. In fact, it's buggy because the preincrement and predecrement operations return the values normally associated with post increment/decrement!

SmartPointer::operator==() is doing an assignment, not a comparison.

Also, assuming that should be SmartPointer::operator=(), you should change it to increment sp->ref before you decrement this->ref, just in case they somehow point to the same ref. Trust me, that's a bug you don't want to have.... :)

SmartPointer<T>::SmartPointer(T *X, ReferenceCounter* ref) creates a new ReferenceCounter for this, but it increments the argument ref.

Why does useCount() return a Date?

Why does SmartPointer have a constructor that takes a Date? It's a template, shouldn't anything related to a specific type like Date be covered by SmartPointer<Date> instead?
Topic archived. No new replies allowed.