簡體   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