zoukankan      html  css  js  c++  java
  • 智能指针shared_ptr

      shared_ptr的作用有如同指针,但会记录有多少个shared_ptrs共同指向一个对象。这便是所谓的引用计数(reference counting)。一旦最后一个这样的指针被销毁,也就是一旦某个对象的引用计数变为0,这个对象会被自动删除。shared_ptr 在 Technique report I 中已经出现,在effective C++ 中提到过shared_ptr 的使用,现在已经成为C++11 的正式成员。shared_ptr 的自动删除功能是一个很好的防止内存泄露的方法,在C++中也称为RAII(Resource Acquisition Is Initialization)。RAII 的一般做法是这样的:在对象构造时获取资源,接着控制对资源的访问使之在对象的生命周期内始终保持有效,最后在对象析构的时候释放资源。借此,我们实际上把管理一份资源的责任托管给了一个对象。这种做法有两大好处:1,我们不需要显式地释放资源。2,采用这种方式,对象所需的资源在其生命期内始终保持有效 —— 我们可以说,此时这个类维护了一个 invariant。这样,通过该类对象使用资源时,就不必检查资源有效性的问题,可以简化逻辑、提高效率。

    下面来看一个简单的例子,涵盖了shared_ptr的基本操作:

    #include <memory> 
    #include <iostream> 
     
    int main() 
    { 
        auto sp1 = std::make_shared<int>(5);
        std::cout << "sp1.unique() == " << std::boolalpha << sp1.unique() << std::endl; 
    
        std::shared_ptr<int> sp2 = sp1; 
        std::cout << "sp1.unique() == " << std::boolalpha << sp1.unique() << std::endl;
    
        std::cout << "sp1.use_count(): "<< sp1.use_count()<<std::endl;
        std::cout << "sp2.use_count(): "<< sp2.use_count()<<std::endl;
    
        std::cout <<"sp1.get(): "<<sp1.get()<<std::endl;
        std::cout <<"sp2.get(): "<<sp2.get()<<std::endl;
    
        sp1.reset();
        
        std::cout << "sp1.use_count(): "<< sp1.use_count()<<std::endl;
        std::cout << "sp2.use_count(): "<< sp2.use_count()<<std::endl;
    
        std::shared_ptr<int> sp3(new int(10));
        std::cout <<"*sp2: "<< *sp2 <<std::endl;
        std::cout <<"*sp3: "<< *sp3 <<std::endl;    
        sp2.swap(sp3);
        std::cout <<"After swap: "<<std::endl;
        std::cout <<"*sp2: "<< *sp2 <<std::endl;
        std::cout <<"*sp3: "<< *sp3 <<std::endl;    
    
    }

    运行结果:

    C:\Windows\system32\cmd.exe /c shared_ptr.exe
    sp1.unique() == true
    sp1.unique() == false
    sp1.use_count(): 2
    sp2.use_count(): 2
    sp1.get(): 001FBEEC
    sp2.get(): 001FBEEC
    sp1.use_count(): 0
    sp2.use_count(): 1
    *sp2: 5
    *sp3: 10
    After swap:
    *sp2: 10
    *sp3: 5
    Hit any key to close this window...

    说明:unique 函数 判断该指针的引用计数是否为1,use_count() 函数返回该指针的引用计数,get()函数返回该指针的值,reset()清除该指针的引用计数,swap()函数交换两个指针的值。

  • 相关阅读:
    使用递归输出某个目录下所有子目录和文件
    如何进行复杂度分析?
    什么是时间复杂度?什么是空间复杂度?
    什么是复杂度?为什么要进行复杂度分析?
    什么是递归?递归的优缺点是什么?
    Executor 如何使用?
    什么是spring boot?为什么要用?
    spring boot核心配置文件是什么?
    @Autowired的作用是什么?
    @RequestMapping的作用是什么?
  • 原文地址:https://www.cnblogs.com/zhuyp1015/p/2633065.html
Copyright © 2011-2022 走看看