C++实现了带有引用计数器的sharedpointer函数

C++ implementing function for sharedpointer with reference counter

本文关键字:计数器 sharedpointer 函数 引用 实现 C++      更新时间:2023-10-16

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

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

我已经为shared_ptr运算符*和->使用了cpp-wiki,并试图用函数实现它,但正在获得表达式必须具有指针类型//表达式必须具有类类型,并且没有运算符"<<"与这些操作数匹配

  1. 所以我认为函数没有返回它们应该返回的内容:-)所以我想我搞砸了。我尝试使用这个->pData和pData.get(),但仍然有相同的错误:-(

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

这是我的主.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&getData和sharedptr*getPointer?

Werner Henze已经在他的评论中说了所有相关的内容,所以这只不过是把所有内容都放在正确的位置。

该错误是由getData()返回指针并在main中用作引用引起的。由于getPointer()已经返回指针,因此修复方法是更改getData()以返回引用:

T& getData() {
return *pData;
}

但是在你的代码中仍然有一些可能的改进:

  • 您没有实现对3个引用的限制,因此您的评论不一致
  • 对指向NULL(默认构造函数)的指针进行引用计数是没有意义的,因为在任何对象上都没有引用
  • operator =的一个常见实现是复制和交换习惯用法。它通常会产生更健壮、更简单的代码

除了第一句话和修复错误后,您可能会要求在代码审查上对您的代码进行彻底审查-小心,损坏的代码或仍未编写的代码显然偏离了主题。。。