一篇文章带你搞定手写shared_ptr智能指针
面试过程中可能经常会遇到手写智能指针的问题,这个问题对同学们的基础要求比较扎实,自我感觉来说比写一些算法题更有意义。
引用计数
首先提到智能指针shared_ptr,那么大家会想到引用计数,我们可以使用一个类,单独完成引用计数这一功能。
共享指针shared_ptr,多个智能指针同时拥有一个对象,当他们全部失效时,这个对象也同时会被删除。
这个shared_count类除构造函数外有三个方法:一个增加计数,一个减少计数,一个获得计数。
增加计数的时候不需要返回计数值,但减少的时候必须返回计数值,因为需要判断是否是最后一个计数。
重载运算符
指针常见的一些运算符的操作需要进行重载:
- 用
*
运算符解引用 - 用
->
运算符指向对象成员 - 像指针一样用在布尔表达式里
所以增加几个成员函数就能解决:
shared_ptr实现
class Type { public: int a = 1; }; class share_count { public: share_count() : _count(1) {} void add_count() { ++_count; } long reduce_count() { return --_count; } long get_count() const { return _count; } private: long _count; }; template<typename T> class smart_ptr { public: smart_ptr(T* ptr = NULL) : m_ptr(ptr) { if (ptr) { m_share_count = new share_count; } } ~smart_ptr() { if (m_ptr && !m_share_count->reduce_count()) { delete m_ptr; delete m_share_count; cout << "~smart_ptr" << endl; } } T& operator*() const { return *m_ptr; } T* operator->() const { return m_ptr; } operator bool() const { return m_ptr; } smart_ptr(const smart_ptr& rhs) noexcept { m_ptr = rhs.m_ptr; m_share_count = rhs.m_share_count; m_share_count->add_count(); } smart_ptr& operator=(const smart_ptr& rhs) noexcept { m_ptr = rhs.m_ptr; m_share_count = rhs.m_share_count; m_share_count->add_count(); return *this; } long use_count() const { if (m_ptr) { return m_share_count->get_count(); } return 0; } private: T* m_ptr; share_count* m_share_count; };#面经笔经#