【发布时间】:2012-05-10 12:40:29
【问题描述】:
我正在尝试使用 std::make_shared 将“this”传递给构造函数
例子:
// headers
class A
{
public:
std::shared_ptr<B> createB();
}
class B
{
private:
std::shared_ptr<A> a;
public:
B(std::shared_ptr<A>);
}
// source
std::shared_ptr<B> A::createB()
{
auto b = std::make_shared<B>(this); // Compiler error (VS11 Beta)
auto b = std::make_shared<B>(std::shared_ptr<A>(this)); // No compiler error, but doenst work
return b;
}
但是这不能正常工作,有什么建议我可以正确地将它作为参数传递吗?
【问题讨论】:
-
“不能正常工作” - 为什么不呢?发生什么了?为什么这么糟糕?请用非概括性描述问题,以便未来的读者可以通过搜索词找到它们。
标签: c++ constructor c++11 this make-shared