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

c++实现简单的智能指针,第1张

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

实现如下的功能:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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 p2(new A());
    cout << p2.use_count() << endl; // 1
    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
    return 0;
}
代码1
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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的演进

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4995070.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存