繁体   English   中英

如何将shared_ptr初始化为结构

[英]How to initialize the shared_ptr to a structure

我有两种结构定义。

struct ST1               
{                        
  int iVar1;               
  int iVar2;             
  float iVar3;
};

struct ST2
{
 std::shared_ptr<ST1> p_sVar4;
};

ST2 structure2; 

我尝试初始化structure2

structure2.p_sVar4 = new ST1();

但那是错的。

如何初始化structure2

使用std :: make_shared:

 structure2.p_sVar4 = std::make_shared<ST1>();

(通常优先于:

structure2.p_sVar4 = std::shared_ptr<ST1>(new ST1)

使用reset

structure2.p_sVar4.reset(new ST1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM