C ++实现共享指针与参考计数器的功能

时间:2017-10-12 07:27:36

标签: c++ c++11 shared-ptr

getPointer()返回此实例的原始指针。 getData()返回实例的原始指针指向的数据。此返回的数据/类型应该是可修改的。 一次最多允许3个引用。

我正在做一些练习任务,以便更好地理解c ++ :-)已经做了大约2个月,现在从基础开始慢到各种其他事情,现在我发现这个任务“重新发明shared_ptr的轮子”和我在尝试实现这些功能时陷入困境,并决定寻求帮助。

我已经将cpp wiki用于shared_ptr operator *和 - >并试图用函数实现它,但是得到表达式必须有指针类型//表达式必须具有类类型而没有运算符“<<”匹配这些操作数

  1. 所以我认为这些功能并没有兑现他们应该做的事情:-)所以我想我搞砸了。我尝试使用this-> pData和pData.get()但仍然是同样的错误: - (

  2. <<错误是100%因为我还没有实现运算符<<在我的sharedpointer.cpp

  3. 这是我的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;
        }
    

    我的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;
    };
    

    更新确切的错误消息

    Severity    Code    Description Project File    Line    Suppression State
    Error (active)  E0349   no operator "<<" matches these operands main.cpp    31  
    Error (active)  E0044   expression must have pointer type   main.cpp    10  
    Error (active)  E0153   expression must have class type main.cpp    13  
    Error (active)  E0153   expression must have class type main.cpp    15  
    Error (active)  E0044   expression must have pointer type   main.cpp    18  
    Error (active)  E0349   no operator "<<" matches these operands main.cpp    28  
    

    这些错误指的是main.cpp中的这一行

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

    问题

    嗯,我想我注意到我应该在我的功能开始时使用我的类名,所以它应该是sharedptr&amp; getData和sharedptr * getPointer?

1 个答案:

答案 0 :(得分:0)

Werner Henze has already said everything relevant in his comments, so this is not much more than taking it all and putting it in its right place.

The error is caused by getData() returning a pointer and being used as a reference in main. As getPointer() already returns the pointer, the fix is to change getData() to return a reference:

T& getData() {
    return *pData;
}

But there are still some possible improvements in your code:

  • you have not implemented the limit to 3 references, so your comments are inconsistent
  • it does not really make sense to count references for a pointer pointing to NULL (default constructor), because there are no references at all on any object
  • a common implementation of the operator = is the copy and swap idiom. It generally leads to more robust and simple code

Apart from the first remark and after fixing the error, you might ask for a thorough review of your code on Code Review - beware, broken code or still not written one is explicitely off topic there...