我对结构有两个定义。
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)
)
关于c++ - 如何将 shared_ptr 初始化为结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35117744/