惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
爱范儿
爱范儿
GbyAI
GbyAI
A
About on SuperTechFans
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Secure Thoughts
Security Latest
Security Latest
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
PCI Perspectives
PCI Perspectives
MyScale Blog
MyScale Blog
罗磊的独立博客
Y
Y Combinator Blog
Know Your Adversary
Know Your Adversary
月光博客
月光博客
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recorded Future
Recorded Future
S
Securelist
T
Tor Project blog
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
P
Privacy & Cybersecurity Law Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Project Zero
Project Zero
T
Tailwind CSS Blog
腾讯CDC
C
Cisco Blogs
T
The Exploit Database - CXSecurity.com
The Hacker News
The Hacker News
F
Full Disclosure
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
美团技术团队
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
C
Cybersecurity and Infrastructure Security Agency CISA
D
Docker
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
O
OpenAI News
Cloudbric
Cloudbric

博客园 - ZefengYao

Markdown 入门与 Word 使用指南 关于崩溃报告的日志以及dump文件 hdu 6223 Infinite Fraction Path 2017南宁现场赛E The Champion ACM-ICPC 2018 南京赛区网络预赛 Sum c语言几个字符串处理函数的简单实现 各种类型排序的实现及比较 随机洗牌算法Knuth Shuffle和错排公式 两个栈实现队列 面试杂题 面试题——栈的压入、弹出顺序 openGL初学函数解释汇总 foj Problem 2107 Hua Rong Dao foj Problem 2282 Wand UVA-1400 Ray, Pass me the dishes! 《挑战程序设计竞赛》 利用后缀数组求最长回文串 Uva 11174 Stand in a Line UVA 11375 Matches poj 3729 Facer’s string
C++ 智能指针的简单实现
ZefengYao · 2018-08-12 · via 博客园 - ZefengYao
  • 智能指针的用处:在c++中,使用普通指针容易造成堆内存的泄露问题,即程序员会忘记释放,以及二次释放,程序发生异常时内存泄漏等问题,而使用智能指针可以更好的管理堆内存。注意,在这里智能指针是一个类而非真正的指针,只是对一个真正的指针进行包装,代理原指针。通过操作符的重载,可以让智能指针和真正的指针有类似的操作。
  • 如何实现:在智能指针析构函数当中,可以对代理的原指针进行delete以及赋空指针等操作,智能指针销毁时,自动调用析构函数,省去了程序员自己管理指针的操作。当多个智能指针代理同一个指针时,我们要引入一个计数器,用来计数智能指针的个数,最后一个销毁的智能指针有义务对原指针进行销毁工作,具体实现代码如下:
template<class T>
class Smart_ptr {
public:
    Smart_ptr(T*ptr);
    Smart_ptr(const Smart_ptr &ptr);//拷贝构造函数
    ~Smart_ptr();//析构函数
    int get_cnt();//获得当前代理原指针的智能指针个数
    Smart_ptr& operator=(const Smart_ptr&ptr);//赋值操作
    T& operator *();//指针操作
    T* operator ->();//指针操作
private:
    T*_ptr;
    int*_cnt;
};
template<class T>
Smart_ptr<T>::Smart_ptr(T*ptr):_ptr(ptr) {//判断参数指针是否为空;不为空,计数+1
    if (_ptr) _cnt = new int(1);
    else _cnt = new int(0);
}
template<class T>
Smart_ptr<T>::Smart_ptr(const Smart_ptr &ptr) {//复制构造函数,复制成功,计数加1
    if (this != &ptr) {
        this->_ptr = ptr._ptr;
        this->_cnt = ptr._cnt;
        (*this->_cnt)++;
    }
}
template<class T>
Smart_ptr<T>::~Smart_ptr() {//若当前的智能指针是最后一个代理指针,负责销毁原指针
    (*this->_cnt)--;
    if ((*this->_cnt) == 0) {
        delete this->_cnt;
        delete this->_ptr;
        _cnt = nullptr;
        _ptr = nullptr;
    }
}
template<class T>
Smart_ptr<T>& Smart_ptr<T>::operator=(const Smart_ptr&ptr) {//1:若赋值前后代理的指针是一样的,直接返回即可2:先判断当前的智能指针是否为最后一个代理原指针的智能指针,若是,销毁原指针;
    if (this->_ptr == ptr._ptr)return *this;//3:当前智能指针将代理一个新的指针
    if (this->_ptr != nullptr) {
        (*this->_cnt)--;
        if (*this->_cnt == 0) {
            delete this->_cnt;
            delete this->_ptr;
        }
    }
    this->_cnt = ptr._cnt;
    this->_ptr = ptr._ptr;
    (*this->_cnt)++;
    return *this;
}
template<class T>
T& Smart_ptr<T>::operator *() {//指针操作
    return *(this->_ptr);
}
template<class T>
T* Smart_ptr<T>::operator->() {//指针操作
    return this->_ptr;
}
template<class T>
int Smart_ptr<T>::get_cnt() {
    return *this->_cnt;
}

main函数:

int main() {
    Smart_ptr<int>sp1(new int(1));
    cout <<"sp1.cnt:"<< sp1.get_cnt() << endl;
    Smart_ptr<int>sp2(sp1);
    cout << "sp1.cnt:" << sp1.get_cnt() << endl;
    Smart_ptr<int>sp3(new int(10));
    cout << "sp3.cnt:" << sp3.get_cnt() << endl;;
    sp3 = sp2;
    cout << "sp3.cnt:" << sp3.get_cnt() << endl;
    return 0;
}

调用结果: