• c++自己实现智能指针:auto_ptr、shared_ptr、unique_ptr


    auto_ptr

     1 template<typename T>
     2 class my_auto_ptr
     3 {
     4 public:
     5     //构造
     6     my_auto_ptr(T* ptr) : m_ptr(ptr) {}
     7     my_auto_ptr(my_auto_ptr<T>& other) {
     8         m_ptr = other.m_ptr;
     9         other.m_ptr = nullptr;
    10     }
    11     my_auto_ptr& operator=(const my_auto_ptr& other) {
    12         if (this != &other)
    13         {
    14             delete m_ptr;
    15             m_ptr = other.m_ptr;
    16             other.m_ptr = nullptr;
    17         }
    18         return *this;
    19     }
    20     ~my_auto_ptr() { delete m_ptr; }
    21 
    22     T* get() {
    23         return m_ptr;
    24     }
    25 
    26     void release() { delete m_ptr; m_ptr = nullptr; }
    27     void reset() { release(); }
    28     T& operator*() const { return *m_ptr; }
    29     T* operator->() const { return m_ptr; }
    30 
    31 protected:
    32     T* m_ptr;
    33 };
    34 
    35 //对void类型特化
    36 template<> 
    37 class my_auto_ptr<void>
    38 {
    39 
    40 };
    View Code

    shared_ptr

     1 template<typename T>
     2 class my_shared_ptr
     3 {
     4 private:
     5     class actual_shared_ptr
     6     {
     7     public:
     8         actual_shared_ptr(T* ptr) : m_ptr(ptr), m_count(1) {}
     9         ~actual_shared_ptr() { delete m_ptr; }
    10 
    11         T* m_ptr;
    12         unsigned int m_count;
    13     };
    14     actual_shared_ptr* m_actual_ptr;
    15 
    16 public:
    17     explicit my_shared_ptr(T* ptr): m_actual_ptr(new actual_shared_ptr(ptr)) {}
    18     my_shared_ptr(my_shared_ptr& other) {
    19         m_actual_ptr = other.m_actual_ptr;
    20         increase();
    21     }
    22     my_shared_ptr& operator=(my_shared_ptr& other) {
    23         if (m_actual_ptr != other.m_actual_ptr)
    24         {
    25             m_actual_ptr = other.m_actual_ptr;
    26             increase();
    27         }
    28         return *this;
    29     }
    30 
    31     ~my_shared_ptr() {
    32         decrease();
    33     }
    34 
    35     unsigned int count() {
    36         return m_actual_ptr->m_count;
    37     }
    38     T& operator* () const {
    39         return *(m_actual_ptr->m_ptr);
    40     }
    41 
    42     T* operator-> () const {
    43         return m_actual_ptr->m_ptr;
    44     }
    45 
    46 protected:
    47     void decrease() {
    48         if (--(m_actual_ptr->m_count) == 0) {
    49             delete m_actual_ptr;
    50             m_actual_ptr = nullptr;
    51         }            
    52     }
    53     void increase() { ++(m_actual_ptr->m_count); }
    54 protected:
    55     T* m_ptr;
    56     unsigned int m_count;
    57 };
    58 
    59 template<>
    60 class my_shared_ptr<void> {};
    View Code

    unique_ptr

     1 class my_uptr_delete
     2 {
     3 public:
     4     template<typename T>
     5     void operator()(T* ptr) const {
     6         if (ptr) delete ptr;
     7     }
     8 };
     9 
    10 class my_uptr_delete_arr
    11 {
    12 public:
    13     template<typename T>
    14     void operator()(T* ptr) const{
    15         if (ptr) delete[] ptr;
    16     }
    17 };
    18 
    19 template<typename T, typename D = my_uptr_delete >
    20 class my_unique_ptr
    21 {
    22 public:
    23     explicit my_unique_ptr(T* ptr = nullptr, const D& d = D()) : m_ptr(ptr), m_del(d){}
    24     ~my_unique_ptr() { m_del(m_ptr); }
    25 
    26     //删除拷贝和赋值
    27     my_unique_ptr(const my_unique_ptr& ptr) = delete;
    28     my_unique_ptr& operator=(const my_unique_ptr& ptr) = delete;
    29 
    30     //可拷贝赋值一个将被销毁的unique_ptr
    31     my_unique_ptr(my_unique_ptr&& rptr) :
    32         m_ptr(rptr.m_ptr), m_del(std::move(rptr.m_del)) {
    33         rptr.m_ptr = nullptr;
    34     }
    35     my_unique_ptr& operator=(my_unique_ptr&& rptr) noexcept {
    36         if (this != &rptr) {
    37             m_del(m_ptr);
    38             m_ptr = rptr.m_ptr;
    39             m_del = std::move(rptr.m_del);
    40             rptr.m_ptr = nullptr;
    41         }
    42         return *this;
    43     }
    44 
    45     //放弃指针的控制权
    46     T* release() {
    47         T* tmp = m_ptr;
    48         m_ptr = nullptr;
    49         return tmp;
    50     }
    51 
    52     /*
    53     u.reset()    释放指向的对象
    54     u.reset(q)   如果提供了内置指针q,就令u指向这个对象
    55     u.reset(nullptr)   将u置空
    56     */
    57     void reset() {
    58         m_del(m_ptr);
    59         m_ptr = nullptr;
    60     }
    61     void reset(T* ptr) {
    62         m_del(m_ptr);
    63         m_ptr = ptr;
    64     }
    65     void swap(my_unique_ptr& ptr) noexcept {
    66         std::swap(m_ptr, ptr.m_ptr);
    67         std::swap(m_del, ptr.m_del);
    68     }
    69 
    70     T* get() const { return m_ptr; }
    71     D& get_deleter() const { return m_del; }
    72     T& operator*() const { return *m_ptr; }
    73     T* operator->() const { return m_ptr; }
    74 protected:
    75     T* m_ptr;
    76     D m_del;
    77 };
    View Code

    测试代码:

     1 void test_auto_ptr()
     2 {
     3     cout << "******test_auto_ptr******" << endl;
     4     my_auto_ptr<int> m_data(new int(1));
     5     cout << *m_data << endl;
     6     
     7     my_auto_ptr<int> m_data1 = m_data;
     8     ++(*m_data1);
     9     cout << *m_data1 << endl;
    10     
    11     my_auto_ptr<int> m_data2(m_data1);
    12     ++(*m_data2);
    13     cout << *m_data2 << endl;
    14 
    15     my_auto_ptr<void> m_void;
    16 }
    17 
    18 void test_shared_ptr()
    19 {
    20     cout << "******test_shared_ptr******" << endl;
    21     my_shared_ptr<int> m_data(new int(1));
    22     cout << *m_data << ",count:" << m_data.count() << endl;
    23 
    24     my_shared_ptr<int> m_data1 = m_data;
    25     ++(*m_data1);
    26     cout << *m_data << "," << *m_data1 << ",count:" << m_data.count() << endl;
    27     cout << "m_data 析构前 count:" << m_data.count() << endl;
    28     m_data.~my_shared_ptr();
    29     cout << "m_data 析构后 count:" << m_data.count() << endl;
    30 
    31     my_shared_ptr<int> m_data2(m_data1);
    32     ++(*m_data2);
    33     cout << *m_data << "," << *m_data1 << "," << *m_data2 << endl;
    34 
    35     my_shared_ptr<void> m_void;
    36 }
    37 
    38 void test_unique_ptr()
    39 {
    40     cout << "******test_unique_ptr******" << endl;
    41     int* p1 = new int(1);
    42     my_unique_ptr<int> m_data(p1);
    43     cout << *m_data << endl;
    44 
    45     int* p2 = new int(2);
    46     m_data.reset();
    47     m_data.reset(p2);
    48     cout << *m_data << endl;
    49 
    50     my_unique_ptr<int> m_data2(new int(3));
    51     m_data.swap(m_data2);
    52     cout << *m_data << endl;
    53 }
    View Code
  • 相关阅读:
    devexpress toolbar 填充整行宽度
    2. Rust的三板斧 安全,迅速,并发
    1. rust的优点
    谈谈我对sku的理解(3)----页面效果
    谈谈我对sku的理解(2)----数据库设计
    谈谈我对sku的理解(1)
    我眼里的奇酷手机360OS
    Oracle中的wm_concat()函数
    获取java本地系统信息 Properties
    java 获取用户的ip都是 127.0.0.1
  • 原文地址:https://www.cnblogs.com/rmdmn/p/14078251.html
Copyright © 2020-2023  润新知