估计大家面试过程中应该都会被问到C++11的shared_ptr是如何实现的,大家应该都能答出来引用计数的概念,但是如果要让你手写一个shared_ptr,你能写出来吗?
最近,我写了一个简单的shared_ptr,在这里分享一波。
首先定义一个主管引用计数的类:
class SharedCount {
public:
SharedCount() : count_{1} {}
void add() { ++count_; }
void minus() { --count_; }
int get() const { return count_; }
private:
std::atomic<int> count_;
};
然后就是SharedPtr类,首先在构造函数中创建SharedCount的对象:
template <typename T>
class SharedPtr {
public:
SharedPtr(T* ptr) : ptr_{ptr}, ref_count_{new SharedCount} {}
SharedPtr() : ptr_{nullptr}, ref_count_{new SharedCount} {}
private:
T* ptr_;
SharedCount* ref_count_;
};
通过构造函数创建出来的SharedPtr引用计数肯定是1,那析构函数怎么实现?无非就是将引用计数减1,如果引用计数最终减到0,则释放所有指针:
template <typename T>
class SharedPtr {
public:
~SharedPtr() { clean(); }
private:
void clean() {
if (ref_count_) {
ref_count_->minus();
if (ref_count_->get() == 0) {
if (ptr_) delete ptr_;
delete ref_count_;
}
}
}
};
然后就是智能指针的关键部分,即在拷贝构造和拷贝赋值的时候将引用计数+1:
template <typename T>
class SharedPtr {
public:
SharedPtr(const SharedPtr& p) {
this->ptr_ = p.ptr_;
this->ref_count_ = p.ref_count_;
ref_count_->add();
}
SharedPtr& operator=(const SharedPtr& p) {
clean();
this->ptr_ = p.ptr_;
this->ref_count_ = p.ref_count_;
ref_count_->add();
return *this;
}
};
处理了拷贝语义,还需要处理移动语义,即实现移动构造和移动赋值函数:
template <typename T>
class SharedPtr {
public:
SharedPtr(SharedPtr&& p) {
this->ptr_ = p.ptr_;
this->ref_count_ = p.ref_count_;
p.ptr_ = nullptr;
p.ref_count_ = nullptr;
}
SharedPtr& operator=(SharedPtr&& p) {
clean();
this->ptr_ = p.ptr_;
this->ref_count_ = p.ref_count_;
p.ptr_ = nullptr;
p.ref_count_ = nullptr;
return *this;
}
};
在移动语义中,引用计数保持不变,同时清空原参数中的指针。
关于共享指针,到这里基本逻辑都已经实现完成,但还需要补充获取裸指针、获取引用计数等接口:
template <typename T>
class SharedPtr {
public:
int use_count() { return ref_count_->get(); }
T* get() const { return ptr_; }
T* operator->() const { return ptr_; }
T& operator*() const { return *ptr_; }
operator bool() const { return ptr_; }
private:
T* ptr_;
SharedCount* ref_count_;
};
这样一个完整的智能指针大体已经实现完成,运行一下看看:
struct A {
A() { std::cout << "A() \n"; }
~A() { std::cout << "~A() \n"; }
};
void test_simple_shared() {
A* a = new A;
SharedPtr<A> ptr(a);
{
std::cout << ptr.use_count() << std::endl;
SharedPtr<A> b = ptr;
std::cout << ptr.use_count() << std::endl;
SharedPtr<A> c = ptr;
std::cout << ptr.use_count() << std::endl;
SharedPtr<A> d = std::move(b);
std::cout << ptr.use_count() << std::endl;
}
std::cout << ptr.use_count() << std::endl;
}
int main() { test_simple_shared(); }
结果为:
A()
1
2
3
3
1
~A()
基本的shared_ptr完成后,再来写点有意思的,不知道大家有没有用过这几个指针转换函数:
template<class T, class U>
std::shared_ptr<T> static_pointer_cast(const std::shared_ptr<U>& r) noexcept;
template<class T, class U>
std::shared_ptr<T> const_pointer_cast(const std::shared_ptr<U>& r) noexcept;
template<class T, class U>
std::shared_ptr<T> dynamic_pointer_cast(const std::shared_ptr<U>& r) noexcept;
template<class T, class U>
std::shared_ptr<T> reinterpret_pointer_cast(const std::shared_ptr<U>& r) noexcept;
我默认大家已经知道这几个函数的作用,这里直接研究一下它的实现:
template <typename T>
class SharedPtr {
public:
private:
template <typename U>
SharedPtr(const SharedPtr<U>& p, T* ptr) {
this->ptr_ = ptr;
this->ref_count_ = p.ref_count_;
ref_count_->add();
}
T* ptr_;
SharedCount* ref_count_;
};
template <typename T, typename U>
SharedPtr<T> static_pointer_cast(const SharedPtr<U>& p) {
T* ptr = static_cast<T*>(p.get());
return SharedPtr<T>(p, ptr);
}
SharedPtr<T>
和SharedPtr<U>
不是一个类,所以上面的代码会稍微有点问题,p无法访问它的private成员变量ref_count,那怎么解决呢?上友元:
template <typename T>
class SharedPtr {
public:
template <typename U>
friend class SharedPtr;
};
上面的代码还是有问题,因为SharedPtr(const SharedPtr<U>
& p, T* ptr)是private,static_pointer_cast无法访问,有两种办法,一是变成public,二是友元,这里还是友元更合理些,添加友元后的代码如下:
template <typename T>
class SharedPtr {
public:
template <typename U>
friend class SharedPtr;
template <typename K, typename U>
friend SharedPtr<K> static_pointer_cast(const SharedPtr<U>& p);
private:
template <typename U>
SharedPtr(const SharedPtr<U>& p, T* ptr) {
this->ptr_ = ptr;
this->ref_count_ = p.ref_count_;
ref_count_->add();
}
T* ptr_;
SharedCount* ref_count_;
};
template <typename T, typename U>
SharedPtr<T> static_pointer_cast(const SharedPtr<U>& p) {
T* ptr = static_cast<T*>(p.get());
return SharedPtr<T>(p, ptr);
}
再测试一下:
struct A {
A() { std::cout << "A() \n"; }
~A() { std::cout << "~A() \n"; }
};
struct B : A {
B() { std::cout << "B() \n"; }
~B() { std::cout << "~B() \n"; }
};
void test_cast_shared() {
B* a = new B;
SharedPtr<B> ptr(a);
{
std::cout << ptr.use_count() << std::endl;
SharedPtr<A> b = static_pointer_cast<A>(ptr);
std::cout << ptr.use_count() << std::endl;
SharedPtr<B> c = ptr;
std::cout << ptr.use_count() << std::endl;
SharedPtr<B> d = ptr;
std::cout << ptr.use_count() << std::endl;
}
std::cout << ptr.use_count() << std::endl;
}
int main() { test_cast_shared(); }
结果为:
A()
B()
1
2
3
4
1
~B()
~A()
上面只实现了static_pointer_cast,其他xxx_pointer_cast的原理类似,大家应该也明白了吧。
C++还有个unique_ptr,这个相对于shared_ptr就简单多了,表示unique语义,没有引用计数的概念,因为不允许拷贝,原理就是禁止调用拷贝构造函数和拷贝赋值函数,直接贴代码吧:
template <typename T>
class UniquePtr {
public:
UniquePtr(T* ptr) : ptr_{ptr} {}
UniquePtr() : ptr_{nullptr} {}
UniquePtr(const UniquePtr& p) = delete;
UniquePtr& operator=(const UniquePtr& p) = delete;
UniquePtr(UniquePtr&& p) {
this->ptr_ = p.ptr_;
p.ptr_ = nullptr;
}
UniquePtr& operator=(UniquePtr&& p) {
clean();
this->ptr_ = p.ptr_;
p.ptr_ = nullptr;
return *this;
}
T* get() const { return ptr_; }
T* operator->() const { return ptr_; }
T& operator*() const { return *ptr_; }
operator bool() const { return ptr_; }
~UniquePtr() { clean(); }
private:
void clean() {
if (ptr_) delete ptr_;
}
T* ptr_;
};
重点其实只有这两个delete:
template <typename T>
class UniquePtr {
public:
UniquePtr(const UniquePtr& p) = delete;
UniquePtr& operator=(const UniquePtr& p) = delete;
};
到这里已经实现了一个简单的shared_ptr和unique_ptr,希望对大家有所帮助,完整代码见这里:
https://github.com/chengxumiaodaren/cpp-learning/blob/master/src/test_shared_ptr.cc
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8