实现一个简单的shared_ptr

本文介绍了一个简单的shared_ptr实现案例,通过源码展示了智能指针的关键步骤,包括引用计数、操作符重载等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

翻看以前的代码的时候发现一个shared_ptr的简单实现。

我记得是网上的一篇例子(好像改了一点),但是又懒得找出处了 ╮(╯▽╰)╭。

觉得这份代码足以用来初步了解shared_ptr的实现了。

 

一般来说,智能指针的实现需要以下步骤:

1.一个模板指针T* ptr,指向实际的对象。

2.一个引用次数(必须new出来的,不然会多个shared_ptr里面会有不同的引用次数而导致多次delete)。

3.重载operator*和operator->,使得能像指针一样使用shared_ptr。

4.重载copy constructor,使其引用次数加一。

5.重载operator=,如果原来的shared_ptr已经有对象,则让其引用次数减一并判断引用是否为零(是否调用delete)。

 然后将新的对象引用次数加一。

6.重载析构函数,使引用次数减一并判断引用是否为零(是否调用delete)。

 

源码如下:

 1 #ifndef __SHARED_PTR_
 2 #define __SHARED_PTR_
 3 
 4 template <typename T>
 5 class shared_ptr {
 6 public:
 7     shared_ptr(T* p) : count(new int(1)), _ptr(p) {}
 8     shared_ptr(shared_ptr<T>& other) : count(&(++*other.count)), _ptr(other._ptr) {}
 9     T* operator->() { return _ptr; }
10     T& operator*() { return *_ptr; }
11     shared_ptr<T>& operator=(shared_ptr<T>& other)
12     {
13         ++*other.count;
14         if (this->_ptr && 0 == --*this->count)
15         {
16             delete count;
17             delete _ptr;
18         }
19         this->_ptr = other._ptr;
20         this->count = other.count;
21         return *this;
22     }
23     ~shared_ptr()
24     {
25         if (--*count == 0)
26         {
27             delete count;
28             delete _ptr;
29         }
30     }
31     int getRef() { return *count; }
32 private:
33     int* count;
34     T* _ptr;
35 };
36 
37 
38 #endif

 

转载于:https://www.cnblogs.com/runnyu/p/5822304.html

以下是一个简单shared_ptr实现,仅用于参考。 ```c++ template<typename T> class shared_ptr { public: shared_ptr() : ptr(nullptr), ref_count(nullptr) {} shared_ptr(T* p) : ptr(p), ref_count(new int(1)) {} shared_ptr(const shared_ptr<T>& other) : ptr(other.ptr), ref_count(other.ref_count) { if (ref_count) ++(*ref_count); } ~shared_ptr() { dispose(); } shared_ptr<T>& operator=(const shared_ptr<T>& other) { if (this != &other) { dispose(); ptr = other.ptr; ref_count = other.ref_count; if (ref_count) ++(*ref_count); } return *this; } T* operator->() const { return ptr; } T& operator*() const { return *ptr; } bool operator==(const shared_ptr<T>& other) const { return ptr == other.ptr; } bool operator!=(const shared_ptr<T>& other) const { return !(*this == other); } bool operator<(const shared_ptr<T>& other) const { return ptr < other.ptr; } bool operator>(const shared_ptr<T>& other) const { return other < *this; } bool operator<=(const shared_ptr<T>& other) const { return !(other < *this); } bool operator>=(const shared_ptr<T>& other) const { return !(*this < other); } bool is_null() const { return ptr == nullptr; } int use_count() const { return ref_count ? *ref_count : 0; } T* get() const { return ptr; } private: T* ptr; int* ref_count; void dispose() { if (ref_count) { --(*ref_count); if (*ref_count == 0) { delete ptr; delete ref_count; } ptr = nullptr; ref_count = nullptr; } } }; ``` 该shared_ptr实现了拷贝构造函数、拷贝赋值运算符、析构函数、箭头运算符、解引用运算符、相等运算符、不等运算符、比较运算符、is_null()方法、use_count()方法和get()方法。 在构造函数中,初始化指针ptr为nullptr,引用计数ref_count为nullptr。在拷贝构造函数中,ptr和ref_count被复制,并且引用计数加1。在析构函数中,如果引用计数不为0,就减1,如果减到0,就删除指针ptr和引用计数ref_count。在赋值运算符中,先dispose旧的shared_ptr,然后复制新的shared_ptr,最后增加引用计数。在箭头运算符和解引用运算符中,返回指针ptr。在相等运算符和不等运算符中,比较指针ptr。在比较运算符中,比较指针ptr。在is_null()方法中,判断指针ptr是否为nullptr。在use_count()方法中,返回引用计数ref_count的值,如果为nullptr,则返回0。在get()方法中,返回指针ptr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值