【发布时间】:2021-12-07 08:15:14
【问题描述】:
如果我有一个Y,它有一个指向X 的成员指针,我如何在X 中创建Y 的实例?
我想过像这样传递它:
#include <memory>
struct X;
struct Y
{
Y(int n, std::shared_ptr<X> x)
: n_(n)
, x_(x)
{}
std::shared_ptr<X> x_;
int n_;
};
struct X
{
void d()
{
auto y = std::make_shared<Y>(20, this);
}
};
int main()
{
return 0;
}
【问题讨论】:
-
另外,出于对问题措辞引起的关注:如果您认为
shared_ptr<>是“默认”使用的指针,那您就大错特错了。 -
这能回答你的问题吗? std::shared_ptr of this
标签: c++ c++14 shared-ptr