lundi 25 mai 2020

Reference Count c++ Smart Pointers

I have class Date, class SmartPointer and class ReferenceCounter. This is what should i do: Add a new property to the class Smart Pointer that should be a pointer to the object ReferenceCounter. This feature will be common to all instances of smart pointers that will point to the same pointer. Initialize this value in the constructor. To "maintain" the reference counter overlay the operator = and implement a copy constructor. Copy both properties and increase the reference counter by 1. Check if someone accidentally wants to copy themselves (the object). Then you just return yourself and don’t copy the object. This is my code:

//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;
    }
unsigned int useCount() {
        return ref->x;
    }
    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;
}

//ReferenceCounter:

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

//main.cpp

int main() {

    SmartPointer<Date> date1(new Date(6, 5, 2020));

    SmartPointer<Date> date2 = date1;

    cout << date1->toString() << endl;
    cout << date2->toString() << endl;
    cout << date1->operator++() << endl;
    cout << date2->operator++() << endl;
    cout << date1->operator==(Date(8, 5, 2019));
    cout << endl << "-----------------------------------" << endl;

    shared_ptr<int> p = make_shared<int>();
    *p = 15;
    cout << *p;
    cout << endl << "-----------------------------------" << endl;
    shared_ptr<Date> d = make_shared<Date>();
    *d = Date(1, 8, 1999);
    cout << *d << endl;
    cout << d->toString();
    cout << endl << "-----------------------------------" << endl;

    std::cout << "reference count: " << date2.useCount() << std::endl;

I am getting errors like this: In member function 'SmartPointer& SmartPointer::operator==(const SmartPointer&)': error: invalid operands of types 'void' and 'int' to binary 'operator==' if(ref->operator--() == 0){ ~~~~~~~~~~~~~~~~~~^~~~ How can i fix this?

Aucun commentaire:

Enregistrer un commentaire