C++手写SharedPtr
#include <iostream>
#include <utility> // for std::move

template <typename T>
class SharedPtr {
private:
    T* ptr;              // 原始指针
    size_t* ref_count;   // 引用计数指针

public:
    // 默认构造函数
    explicit SharedPtr(T* p = nullptr) 
        : ptr(p), ref_count(p ? new size_t(1) : nullptr) {
        if (ptr) std::cout << "SharedPtr ctor, count=" << *ref_count << "\n";
    }

    // 拷贝构造函数
    SharedPtr(const SharedPtr& other) 
        : ptr(other.ptr), ref_count(other.ref_count) {
        if (ref_count) {
            ++(*ref_count);
            std::cout << "SharedPtr copy, count=" << *ref_count << "\n";
        }
    }

    // 移动构造函数
    SharedPtr(SharedPtr&& other) noexcept 
        : ptr(other.ptr), ref_count(other.ref_count) {
        other.ptr = nullptr;
        other.ref_count = nullptr;
        std::cout << "SharedPtr move ctor\n";
    }

    // 拷贝赋值运算符
    SharedPtr& operator=(const SharedPtr& other) {
        if (this != &other) {
            release();
            ptr = other.ptr;
            ref_count = other.ref_count;
            if (ref_count) {
                ++(*ref_count);
                std::cout << "SharedPtr copy assign, count=" << *ref_count << "\n";
            }
        }
        return *this;
    }

    // 移动赋值运算符
    SharedPtr& operator=(SharedPtr&& other) noexcept {
        if (this != &other) {
            release();
            ptr = other.ptr;
            ref_count = other.ref_count;
            other.ptr = nullptr;
            other.ref_count = nullptr;
            std::cout << "SharedPtr move assign\n";
        }
        return *this;
    }

    // 析构函数
    ~SharedPtr() {
        release();
    }

    // 获取原始指针
    T* get() const { return ptr; }

    // 解引用
    T& operator*() const { return *ptr; }
    T* operator->() const { return ptr; }

    // 获取引用计数
    size_t use_count() const { return ref_count ? *ref_count : 0; }

private:
    void release() {
        if (ref_count) {
            if (--(*ref_count) == 0) {
                std::cout << "Deleting managed object\n";
                delete ptr;
                delete ref_count;
            } else {
                std::cout << "Release, count=" << *ref_count << "\n";
            }
        }
        ptr = nullptr;
        ref_count = nullptr;
    }
};

// 测试用例
struct Foo {
    Foo(int v) : value(v) { std::cout << "Foo ctor\n"; }
    ~Foo() { std::cout << "Foo dtor\n"; }
    int value;
};

int main() {
    SharedPtr<Foo> sp1(new Foo(42));
    SharedPtr<Foo> sp2(sp1);              // 拷贝构造
    SharedPtr<Foo> sp3(std::move(sp1));   // 移动构造
    SharedPtr<Foo> sp4;
    sp4 = sp2;                            // 拷贝赋值
    sp4 = std::move(sp3);                 // 移动赋值

    std::cout << "sp2 count=" << sp2.use_count() << "\n";
    std::cout << "sp4 count=" << sp4.use_count() << "\n";
}
感谢阅读!如有疑问请留言
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇