智能指针

智能指针

//
// Created by lyx on 2025/8/2.
//

#ifndef LEARNC___SIMPLESHAREDPTR_H
#define LEARNC___SIMPLESHAREDPTR_H

struct ControlBlock{
    int ref_count;
    ControlBlock() :ref_count(1){}
};


template <typename T>
class SimpleSharedPtr {
private:
    T *ptr;
    ControlBlock * control;
    void release(){
        if(control){
            --control->ref_count;
            if(control->ref_count == 0){
                delete ptr;
                ptr = nullptr;
                delete control;
                control = nullptr;
            }
        }
    }
public:
    SimpleSharedPtr():ptr(nullptr),control(nullptr){}
    explicit SimpleSharedPtr(T *p) : ptr(p){
        if(p){
            control = new ControlBlock();
        }else{
            control = nullptr;
        }
    }
    ~SimpleSharedPtr(){
        if(ptr){
            release();
        }
    }

    // 拷贝构造 s2(s1)
    SimpleSharedPtr(const SimpleSharedPtr & s) : ptr(s.ptr), control(s.control){
        if(control){
            ++ control->ref_count;
        }
    }

    // 拷贝赋值 s2(new student()); s2 = s1;
    SimpleSharedPtr & operator = (const SimpleSharedPtr &s){
        if(this != &s){
            release();
            ptr = s.ptr;
            control = s.control;
            if(control){
                ++ control->ref_count;
            }
        }
        return * this;
    }

    // 移动构造 SimpleSharedPtr s2(std::move(s1))
    SimpleSharedPtr(SimpleSharedPtr && other) noexcept : ptr(other.ptr), control(other.control){
        other.ptr = nullptr;
        other.control = nullptr;
    }

    // 移动赋值 SimpleSharePtr s2; s2 = std::move(s1);
    SimpleSharedPtr & operator = (SimpleSharedPtr && other) noexcept{
        if(this != &other){
            release();
            ptr = other.ptr;
            control = other.control;
            other.ptr = nullptr;
            other.control = nullptr;
        }
        return *this;
    }

    // 重载运算符 SimpleSharePtr s2(new student()); s2 -> name = "tom"; s2.ptr -> name = "tom";
    T * operator -> () const {
        return ptr;
    }

    // simpleSharePtr s2(new student()); (*s2).name = "tom"; (*s2.ptr).name = "tom";
    T & operator * () const {
        return *ptr;
    }

    // Student * raw_s2 = s2.get();
    T * get() const{
        return ptr;
    }

    //
    int use_count() const{
        return control ? control->ref_count : 0;
    };

    // s2.reset(new Student());
    // s2.reset();
    void reset(T *p = nullptr){
        release();
        ptr = p;
        if(p){
            control = new ControlBlock();
        } else {
            control = nullptr;
        }
    }

};


#endif //LEARNC___SIMPLESHAREDPTR_H

//
// Created by lyx on 2025/8/2.
//

#include <iostream>
#include "SimpleSharedPtr.h"
#include "Student.h"

int main(){
    std::cout << "Creating default shared pointer" << std::endl;
    SimpleSharedPtr<Student> ptr1;
    std::cout << "Ptr1 use_count : " << ptr1.use_count() << std::endl;

    std::cout << "Creating shared pointer with Student" << std::endl;
    SimpleSharedPtr<Student> ptr2(new Student("Tom", 20));
    std::cout << "Ptr2 use_count : " << ptr2.use_count() << std::endl;

    std::cout << "Creating shared from another shared pointer" << std::endl;
    SimpleSharedPtr<Student> ptr3(ptr2);
    std::cout << "Ptr3 use_count : " << ptr3.use_count() << std::endl;
    std::cout << "Ptr2 use_count : " << ptr2.use_count() << std::endl;

    std::cout << "-----------------" << std::endl;
    ptr1 = ptr3;
    std::cout << "Ptr1 use_count : " << ptr1.use_count() << std::endl;
    std::cout << "Ptr3 use_count : " << ptr3.use_count() << std::endl;
    std::cout << "Ptr2 use_count : " << ptr2.use_count() << std::endl;
    std::cout << "-----------------" << std::endl << std::endl;

    ptr2.reset(new Student("Ray", 23));
    std::cout << "Ptr2 use_count : " << ptr2.use_count() << std::endl;
    std::cout << "Ptr3 use_count : " << ptr3.use_count() << std::endl;
    std::cout << "Ptr1 use_count : " << ptr1.use_count() << std::endl;
    
    return 0;
}
posted @ 2025-08-05 17:57  xiaoluosibky  阅读(6)  评论(0)    收藏  举报