栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++智能指针的简单实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++智能指针的简单实现


title: 智能指针的简单实现
date: 2022-05-06 09:31:04
tags: [智能指针]
categories: [c++]

我的个人博客


shared_ptr的简单实现(非线程安全)

主要包括以下成员函数:

  1. 构造函数

  2. 析构函数

  3. 拷贝构造函数

  4. operator=()

  5. operator*()

  6. operator->()

#include 
#include 

using namespace std;

template
class shared_ptr{
private:
    size_t* p_count;
    T* ptr;
public:
    explicit shared_ptr(T* _ptr = nullptr):ptr(_ptr){
        if(ptr) p_count = new size_t(1);
        else p_count = new size_t(0);
    }

    shared_ptr(const shared_ptr& rhs){
        ptr = rhs.ptr;
        p_count = rhs.p_count;
        (*p_count)++;
    }

    
    shared_ptr& operator=(const shared_ptr& rhs){
        
        if(this == &rhs) return *this;

        
        if(ptr){
            (*p_count)--;
            if((*p_count)==0){
                delete ptr;
                delete p_count;
            }
        }

        
        ptr = rhs.ptr;
        p_count = rhs.p_count;
        (*p_count)++;
        return *this;
    }

    
    T& operator*(){
        assert(ptr);
        return *ptr;
    }

    
    T* operator->(){
        assert(ptr);
        return ptr;
    }

    size_t use_count() const {
        return *p_count;
    }

    ~shared_ptr(){
        (*p_count)--;
        if((*p_count)==0){
            delete ptr;
            delete p_count;
        }
    }
};

class test{
public:
    int a = 1;
};

int main(){
    shared_ptr p1(new test); // 默认构造函数
    cout<a< p2(p1); // 拷贝构造函数
        cout<<"p2:"< p3 = p2; // 拷贝构造函数
        cout<<"p2:"< p4(new test);
        cout<<"Before p4:"< 
unique_ptr的简单实现 

主要包括一下成员函数:

  1. 构造函数;

  2. 移动构造函数;

  3. 析构函数;

  4. 禁用拷贝构造函数;

  5. 禁用拷贝赋值函数 operator=();

  6. reset(): 释放源资源,指向新资源;

  7. release(): 返回资源,放弃对资源的管理;

  8. get(): 返回资源,只是公外部使用,依然管理资源;

  9. operator bool(): 是否持有资源;

  10. operator*();

  11. operator->();

#include 
#include 

template
class unique_ptr{
private:
	T* ptr;
    
public:
    unique_ptr(T* _ptr):ptr(_ptr){};
    
    unique_ptr(unique_ptr&& rhs):ptr(rhs.release()){};
        
    ~unique_ptr(){
        if(ptr) delete ptr;
        ptr = nullptr;
    }
    
    
    unique_ptr(const unique_ptr& rhs) = delete;
    unique_ptr& operator=(const unique_ptr& rhs) = delete;
    
public:
    
    void reset(T* _ptr){
        if(ptr) delete ptr;
        ptr = _ptr;
    }
    
    
    T* release(){
        T* pTemp = ptr;
        ptr = nullptr;
        return pTemp;
    }
    
    T* get(){
        return ptr;
    }
    
public:
    
    explicit operator bool() const{
        return ptr != nullptr;
    }
    
    T& operator*(){
        assert(ptr);
        return *ptr;
    }
    
    T* operator->(){
        assert(ptr);
        return ptr;
    }   
};

class test{
public:
    int a = 1;
};

int main(){
    unique_ptr x(new test);
    cout<<"x->a:"<<(*x).a< y = move(x);
    //test* aaa = x.get();
    if(!x) cout<<"Moved x!!"<a:"<a< 

参考文献:

[1] https://zhuanlan.zhihu.com/p/344953368

[2] https://www.ccppcoding.com/archives/202

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/867305.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号