首页 > 其他分享 >shared_ptr释放

shared_ptr释放

时间:2022-09-06 21:11:48浏览次数:48  
标签:std 释放 name age Person shared ptr

#include <iostream>
#include <memory>
using namespace std;

class Person
{
public:
   Person()
   {
      cout << "Person()构造" << endl;
   }

   ~Person()
   {
      cout << "~Person()析构" << endl;
   }


   Person(const Person &p)
   {
       cout << "Person()拷贝构造" << endl;
       this->name = p.name;
       this->age = p.age;
   }

   const Person& getPerson()
   {
      return *this;
   }

   string name;
   int age;
};

int main()
{
    std::shared_ptr<Person> p2;

    {
        std::shared_ptr<Person> p1(new Person);
        cout << p1.use_count() << endl;
        cout << p1.get() << endl;
        p2 = p1;
        cout << p1.use_count() << endl;
    }
    {
        cout << p2.use_count() << endl;
        cout << p2 << endl;

        p2.reset();
        cout << p2.use_count() << endl;
    }

    return 0;
}
$ g++ shared_ptr.cpp -std=c++11
$ ./a.out 
Person()构造
1
0x22ccc20
2
1
0x22ccc20
~Person()析构
0

标签:std,释放,name,age,Person,shared,ptr
From: https://www.cnblogs.com/zhangxuechao/p/16663314.html

相关文章

  • 360数科王继平:释放技术能量,打造行业第二曲线
    短短几年时间,互联网金融经历了一场加速跑,行业正在进入新的发展阶段。近日,360数科举办了第二届开放日,展示其在金融科技业务全环节精细化运营成果的同时,也重新定位了一家技......
  • E10——Sharedata 使用
         通过ActiveObject.RL_[关联实体名].字段名来获取访问界面上的实体关联的字段  如下例子,大概就是通过已经设好的ShareData关系,通过关系名称取到对应......
  • make_shared
    template<classT,class...Args>shared_ptr<T>make_shared(Args&&...args);Makeshared_ptrAllocatesandconstructsanobjectoftypeTpassingargsto......
  • 2-PageCachechan产生释放及优化
    2-PageCache生产释放及优化观察PageCachepagecache,又称pcache,其中文名称为页高速缓冲存储器页缓存PageCache有关的场景故障场景服务器的load飙高;服务器的I/O......
  • 并发多线程10 future其他成员函数、shared_future、atomic
    第十节future其他成员函数、shared_future、atomic一、std::future的成员函数1、std::future_statusstatus=result.wait_for(std::chrono::seconds(几秒));卡住当前......
  • 在 React 中释放 Web 组件的力量
    在React中释放Web组件的力量Photoby法提赫on不飞溅假设您有一个项目,要求您在React应用程序中使用Web组件。你如何使用该组件的状态?如何访问它的方法和属......
  • 钓鱼攻击第一弹-释放文件
    钓鱼攻击第一弹-释放文件之后所有文章发至《熊猫安全》公众号上获取当前路径#include<stdio.h>#include"direct.h"#defineMAX_SIZE255intmain(intargc,const......
  • 1- ? str::tr1、variadic templates、nullptr、auto
    1、str::tr1命名空间其中包括shared_ptr和regex,他们都被搬到str命名空间中了 2、https://isocpp.org/blog/2014/03/compiler-support-for-c11-and-c14可以查看支持的......
  • 万物皆可集成系列:低代码释放用友U8+深度价值(2)—数据拓展应用
    在上一篇内容我们介绍了如何利用低代码开发套件实现低代码应用与U8+系统的对接集成,本次给大家带来的是如何将用友U8+系统中的数据进行价值扩展和实际应用。我们以生产物料......
  • 解决linux删除文件空间未释放
    问题描述linux系统,磁盘占用率很高,删除一些正在使用的大文件,实际空间未得到释放,文件的句柄未被关闭问题解决lsof|grepdelete//删除的文件进程kill-9piddf-h......