首页 > 其他分享 >make_shared

make_shared

时间:2022-09-05 09:57:23浏览次数:61  
标签:std val make object shared ptr

template <class T, class... Args>
  shared_ptr<T> make_shared (Args&&... args);
Make shared_ptr

Allocates and constructs an object of type T passing args to its constructor, and returns an object of type shared_ptr<T> that owns and stores a pointer to it (with a use count of 1).

This function uses ::new to allocate storage for the object. A similar function, allocate_shared, accepts an allocator as argument and uses it to allocate the storage.

A shared_ptr object that owns and stores a pointer to a newly allocated object of type T.

 

int main()
{
    A a;
    a.val = 100;
    using namespace std;
    std::shared_ptr<A> p0 = make_shared<A>(a);
    std::shared_ptr<A> p1 = make_shared<A>(a);
    std::shared_ptr<A> p2 = p0;
    std::shared_ptr<A> p3 = p1;
    p2->val = 200;
    printf("A.val :%d\n", a.val);
    printf("A.val :%d\n", p0->val);
    getchar();
}

标签:std,val,make,object,shared,ptr
From: https://www.cnblogs.com/zhoug2020/p/16657023.html

相关文章

  • cmake add_library编译链接静态库cmakelists
      本篇文章我们来编写CMakeLists.txt使用cmake的add_library的构建静态库,并使用target_link_libraries链接指定的静态库。cmake的linuxwindows和linux环境的准备可以......
  • 一文了解Makefile
    本篇翻译自《LearnMakefilesWiththetastiestexamples》,翻译主要是意译,加入了一些个人理解。熟练英文的朋友请直接阅读原文。链接见:https://makefiletutorial.com/#......
  • cmake和makefile区别和cmake指定编译器(cmake -G)
    一cmake和makefile区别要说明区别,我们先要区分下面三类工具:1.项目构建生成工具首先cmake是项目构建生成工具,cmake的代码可以与平台系统和编译器无关。类似cmake的工具......
  • cmake是什么,为什么现在都用cmake,cmake编译原理和跨平台示例
    一cmake是什么? CMake是一个开源、跨平台的工具系列,是用来构建、测试和打包软件。CMake使用平台无关的配置文件来控制软件编译过程,并生成可在您选择的编译器环境中使用......
  • CMAKE 调用交叉编译器(CMAKE使用)
    CMAKE命令使用时需要与CMakeLists.txt在不同的目录下,一般是在CMakeList.txt文档所在的目录下创建一个build文件夹,然后cd到build文件夹,执行cmake..,此时会根据CMakeLists.......
  • [转]CMake与Make最简单直接的区别
    写程序大体步骤为:1.用编辑器编写源代码,如.c文件。2.用编译器编译代码生成目标文件,如.o。3.用链接器连接目标代码生成可执行文件,如.exe。但如果源文件太多,一个一个编译......
  • 并发多线程10 future其他成员函数、shared_future、atomic
    第十节future其他成员函数、shared_future、atomic一、std::future的成员函数1、std::future_statusstatus=result.wait_for(std::chrono::seconds(几秒));卡住当前......
  • ubuntu 16.04 cmake升级
    ubuntu16.04默认安装的cmake版本为3.5,但有时编译一些包时需要更高的版本,需要升级cmake。千万别执行下面的命令,这样会把之前用cmake编译好的包都给卸载掉,包括ros。......
  • 在visual studio中使用cmake生成dll中的坑
    最近改用visualstudio写c++,把我之前基于MinGW的代码库clon下来在vs中跑,结果不能运行,因为链接器总是莫名奇妙地链接的我想要生成的dll文件同名的lib文件如下图所示文件结......
  • 在VS2019中配置OpenGL环境。(使用CMake方法)
    网上一大堆VS下配置OpenGL环境的,但是这些方法都是基于VS空项目,并没有利用Cmake来构建。而我之前的代码都是在Linux下使用cmake构建,所以为了更快的在VS下调试运行我的程序,所......