C++实现了带有引用计数器的sharedpointer函数
C++ implementing function for sharedpointer with reference counter
getPointer()返回此实例的原始指针。getData()返回实例的原始指针所指向的数据。此返回的数据/类型应该是可修改的。一次最多应允许3个引用。
我正在做一些练习任务,以更好地理解c++:-)已经做了大约2个月了,现在从基础到其他各种事情都开始缓慢,现在我发现这个任务是"重新发明shared_ptr的轮子",我在尝试实现这些功能时遇到了困难,决定寻求帮助。
我已经为shared_ptr运算符*和->使用了cpp-wiki,并试图用函数实现它,但正在获得表达式必须具有指针类型//表达式必须具有类类型,并且没有运算符"<<"与这些操作数匹配
-
所以我认为函数没有返回它们应该返回的内容:-)所以我想我搞砸了。我尝试使用这个->pData和pData.get(),但仍然有相同的错误:-(
-
<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 =
的一个常见实现是复制和交换习惯用法。它通常会产生更健壮、更简单的代码
除了第一句话和修复错误后,您可能会要求在代码审查上对您的代码进行彻底审查-小心,损坏的代码或仍未编写的代码显然偏离了主题。。。
- 循环在计数器中不起作用
- python集合的C++等价物是什么.计数器
- Python 集合.计数器,如何避免重复查找
- 请解释字谜的代码,我看不懂计数器数组,每个值已经是0
- std::shared_ptr 自定义参考计数器
- 英特尔 TBB 程序不会终止,可能会误用参考计数器
- 不正确的比较和交换计数器输出用于快速排序功能
- OpenSSL API,使用GCM(伽罗瓦计数器模式)进行AES加密
- C++ 在循环中添加计数器变量并再次初始化其值
- 需要帮助才能在C++中创建类计数器
- 漂亮的计数器习语的错误或格式错误的静态订单惨败?
- 几乎总是自动和带计数器的循环
- 线程使用计数器C++
- 为什么使用 2 个嵌套循环 O(n^2) 复杂度来解决二和问题,当只改变循环计数器逻辑时运行得更快?
- 对于循环计数器不递增
- C++输出的就地计数器
- 在两个值之间嵌入C计数器
- 如何在C++中创建 2d 数组,其中一部分包含循环计数器,另一部分包含数字列表?
- 当输出达到某个值时,如何在 c++ 中中断计数器循环
- OpenCV CV 查找单应断言错误计数器 = > 4