自己实现一个智能指针,替代stl中的智能指针

实现如下的功能:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <memory>
using namespace std;

class A {
public :
    A() { 
        cout << "default constructor" << endl;
    }
    int x, y;
    ~A() {
        cout << "destructor" << endl;
    }
};

int main() {
    A *p1 = new A();
    p1 = nullptr;
    shared_ptr<A> p2(new A());
    cout << p2.use_count() << endl; // 1
    shared_ptr<A> p3 = p2;
    p2->x = 123; p2->y = 456;
    (*p2).x = 456;
    cout << p2.use_count() << endl; // 2
    p2 = nullptr; // 自动析构
    cout << p3.use_count() << endl; // 1
    return 0;
}

代码1

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <memory>
using namespace std;
namespace haizei {

class A {
public :
    A() { 
        cout << "default constructor" << endl;
    }
    int x, y;
    ~A() {
        cout << "destructor" << endl;
    }
};

class shared_ptr{
public:
    shared_ptr();
    shared_ptr(A *);
    shared_ptr(const shared_ptr &);
    int use_count();
    A *operator->();
    A &operator*();
    ~shared_ptr();
    shared_ptr &operator=(const shared_ptr &);
private:
    int *cnt;
    A *obj;
};

shared_ptr::shared_ptr() : obj(nullptr), cnt(nullptr) {}
shared_ptr::shared_ptr(A *obj) : obj(obj), cnt(new int(1)) {}
shared_ptr::shared_ptr(const shared_ptr &p) : obj(p.obj), cnt(p.cnt) { *p.cnt += 1; }
int shared_ptr::use_count() {return cnt ? *cnt : 0;}
A *shared_ptr::operator->() {return obj;}
A &shared_ptr::operator*() {return *obj;}
shared_ptr::~shared_ptr() {
    if (cnt != nullptr) {
        *cnt -= 1;
        if (*cnt == 0) delete obj;
        obj == nullptr;
        cnt == nullptr;
    }
}

shared_ptr &shared_ptr::operator=(const shared_ptr &p) {
    if (this->obj != p.obj) {
        if (this->cnt != nullptr) {
            *(this->cnt) -= 1;
            if (*(this->cnt) == 0) {
                delete this->obj;
                delete this->cnt;
            }
        }
        this->obj = p.obj;
        this->cnt = p.cnt;
        if (this->cnt != nullptr) {
            *(this->cnt) += 1;
        }
    }
    return *this;
}
} // end of haizei

int main() {
    haizei::A *p1 = new haizei::A();
    p1 = nullptr;
    haizei::shared_ptr p2(new haizei::A());
    cout << p2.use_count() << endl; // 1
    haizei::shared_ptr p3 = p2;
    p2->x = 123; p2->y = 456;
    (*p2).x = 456;
    cout << p2.use_count() << endl; // 2
    p2 = nullptr; // 自动析构
    cout << p3.use_count() << endl; // 1
    p2 = p3;
    cout << p3.use_count() << endl;
    return 0;
}

代码2

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <memory>
using namespace std;
namespace haizei {

class A {
public :
    A() { 
        cout << "default constructor" << endl;
    }
    int x, y;
    ~A() {
        cout << "destructor" << endl;
    }
};

class shared_ptr{
public:
    shared_ptr();
    shared_ptr(A *);
    shared_ptr(const shared_ptr &);
    int use_count();
    A *operator->();
    A &operator*();
    ~shared_ptr();
    shared_ptr &operator=(const shared_ptr &);
private:
    void decrease_by_one();
    void increase_by_one();
    int *cnt;
    A *obj;
};

shared_ptr::shared_ptr() : obj(nullptr), cnt(nullptr) {}
shared_ptr::shared_ptr(A *obj) : obj(obj), cnt(new int(1)) {}
shared_ptr::shared_ptr(const shared_ptr &p) : obj(p.obj), cnt(p.cnt) { increase_by_one(); }
int shared_ptr::use_count() {return cnt ? *cnt : 0;}
A *shared_ptr::operator->() {return obj;}
A &shared_ptr::operator*() {return *obj;}
shared_ptr::~shared_ptr() {
    this->decrease_by_one();
    this->obj = nullptr;
    this->cnt = nullptr;
}

void shared_ptr::decrease_by_one() {
    if (this->cnt != nullptr) {
        *(this->cnt) -= 1;
        if (*(this->cnt) == 0) {
            delete this->obj;
            delete this->cnt;
        }
    }
    return ;
}
void shared_ptr::increase_by_one() {
    if (cnt != nullptr) {
        cnt[0] += 1;
    }
    return;
}


shared_ptr &shared_ptr::operator=(const shared_ptr &p) {
    if (this->obj != p.obj) {
        this->decrease_by_one();
        obj = p.obj;
        cnt = p.cnt;
        increase_by_one();
    }
    return *this;
}
} // end of haizei

int main() {
    haizei::A *p1 = new haizei::A();
    p1 = nullptr;
    haizei::shared_ptr p2(new haizei::A());
    cout << p2.use_count() << endl; // 1
    haizei::shared_ptr p3 = p2;
    p2->x = 123; p2->y = 456;
    (*p2).x = 456;
    cout << p2.use_count() << endl; // 2
    p2 = nullptr; // 自动析构
    cout << p3.use_count() << endl; // 1
    p2 = p3;
    cout << p3.use_count() << endl;
    return 0;
}

好好琢磨下,代码1到代码2的演进


版权声明:本文为haojie_duan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/haojie_duan/article/details/121091288