jeudi 12 octobre 2017

C++ implementing function for sharedpointer with reference counter

getPointer() return the raw pointer of this instance. getData() returns the data where the instance's raw pointer points to. This returned data/type should be modifiable. max 3 references should be allowed at a time.

I am doing some practice tasks to better understand c++ :-) been doing them for about 2 months now starting slow for basics to all kinds of other things and now i found this task to "reinvent the wheel of shared_ptr" and i got stuck when trying to implement the functions and decided i would ask for help.

I have used the cpp wiki for shared_ptr operator* and -> and tried to implement that with the functions but am getting expression must have pointer type // expression must have class type and no operator"<<" matches these operands

  1. so what i think is that the functions are not delivering back what they are supposed to :-) so i guess i messed up. I tried using this->pData and also pData.get() but still same errors :-(

  2. the << error is 100% because i have yet to implement the operator<< in my sharedpointer.cpp

sharedpointer.cpp

#include "Cat.hpp"
using namespace std;

class RC // Reference count
{
private:
    int count; // Reference count
public:
    void AddRef()
    {
        // Increment the reference count
        count++;
    }

    int Release()
    {
        // Decrement the reference count and
        // return the reference count.
        return --count;
    }
};



template < typename T > class sharedPtr
{
private:
    T*    pData;       // pointer
    RC* reference; // Reference count

public:
    sharedPtr() : pData(0), reference(0)
    {
        // Create a new reference 
        reference = new RC();
        // Increment the reference count
        reference->AddRef();
    }

    sharedPtr(T* pValue) : pData(pValue), reference(0)
    {
        // Create a new reference 
        reference = new RC();
        // Increment the reference count
        reference->AddRef();
    }

    sharedPtr(const sharedPtr<T>& sharedPtr) : pData(sharedPtr.pData), reference(sharedPtr.reference)
    {
        // Copy constructor

        reference->AddRef();
    }

    ~sharedPtr()
    {
        // Destructor

        if (reference->Release() == 0)
        {
            delete pData;
            delete reference;
        }
    }

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

    T* operator-> ()
    {
        return pData;
    }

    sharedPtr<T>& operator = (const sharedPtr<T>& sharedPtr)
    {
        // Assignment operator
        if (this != &sharedPtr) // Avoid self assignment
        {


            if (reference->Release() == 0)
            {
                delete pData;
                delete reference;
            }

            // Copy the data and reference pointer
            // and increment the reference count
            pData = sharedPtr.pData;
            reference = sharedPtr.reference;
            reference->AddRef();
        }
        return *this;
    }

    T* getData() {
        return pData;


    }
    T* getPointer() {
        return pData;

    }
};

This is my main.cpp

#include "sharedPtr.hpp"
#include "Cat.hpp"
#include <iostream>

    int main(void)
    {
        sharedPtr<Cat> newCat(new Cat(0, "Ferrari"));

        // should print "Ferrari"
        std::cout << newCat.getPointer()->getName() << std::endl;

        // should also print "Ferrari"
        std::cout << newCat.getData().getName() << std::endl;

        newCat.getData().score = 50;

        // should printf "50", as it was just assigned before
        std::cout << newCat.getPointer()->score << std::endl;

        // make some copies
        sharedPtr<Cat> copyOfnewCat = newCat;
        sharedPtr<Cat> copyOfnewCat2 = newCat;

        // this copy should fail
        sharedPtr<Cat> copyOfnewCat3 = newCat;

        // should be nullptr
        std::cout << copyOfnewCat3.getPointer() << std::endl;

        // should be something other than 0 and equal
        std::cout << "copy2 pointer: " << copyOfnewCat2.getPointer() << " copy1 pointer: " << copyOfnewCat.getPointer() << std::endl;
        copyOfnewCat2.getData().score = 40;

        // should be 40 now 
        std::cout << newCat.getPointer()->score << std::endl;
        sharedPtr<Cat> copyOfnewCat4(newCat);

        // should be nullptr
        std::cout << copyOfnewCat4.getPointer() << std::endl;
    }

My Cat.cpp

#include <string>

class Cat
{
public:
    Cat(unsigned int w_score, const std::string& w_name) :
        score(w_score),
        name(w_name)
    {}
    std::string getName()
    {
        return name;
    }

    unsigned int score;
private:
    std::string name;
};

Aucun commentaire:

Enregistrer un commentaire