如何初始化作为类成员的shared_ptr?

发布于 2024-09-15 17:22:24 字数 442 浏览 7 评论 0原文

我不确定初始化作为类成员的 shared_ptr 的好方法。你能告诉我,我在C::foo()中选择的方式是否可以,或者有更好的解决方案吗?

class A
{
  public:
    A();
};

class B
{
  public:
    B(A* pa);
};

class C
{
    boost::shared_ptr<A> mA;
    boost::shared_ptr<B> mB;
    void foo();
};

void C::foo() 
{
    A* pa = new A;
    mA = boost::shared_ptr<A>(pa);
    B* pB = new B(pa);
    mB = boost::shared_ptr<B>(pb);
}

I am not sure about a good way to initialize a shared_ptr that is a member of a class. Can you tell me, whether the way that I choose in C::foo() is fine, or is there a better solution?

class A
{
  public:
    A();
};

class B
{
  public:
    B(A* pa);
};

class C
{
    boost::shared_ptr<A> mA;
    boost::shared_ptr<B> mB;
    void foo();
};

void C::foo() 
{
    A* pa = new A;
    mA = boost::shared_ptr<A>(pa);
    B* pB = new B(pa);
    mB = boost::shared_ptr<B>(pb);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅沫记忆 2024-09-22 17:22:24

您的代码非常正确(它可以工作),但是您可以使用初始化列表,如下所示:

C::C() :
  mA(new A),
  mB(new B(mA.get())
{
}

这更加正确且安全。

无论出于何种原因,如果 new Anew B 抛出异常,则不会发生泄漏。

如果 new A 抛出,则不会分配内存,并且异常也会中止您的构造函数。什么也没建造。

如果 new B 抛出,异常仍会中止您的构造函数:mA 将被正确析构。

当然,由于 B 的实例需要一个指向 A 实例的指针,因此成员的声明顺序很重要

在您的示例中,成员声明顺序是正确的,但如果颠倒过来,那么您的编译器可能会抱怨 mBmAmB 实例化之前初始化 可能会失败(因为 mA 尚未构造,因此调用 mA.get() 会调用未定义的行为)。


我还建议您使用 shared_ptr 而不是 A* 作为 B 构造函数的参数(if 这是有道理的,如果你能接受一点点开销的话)。可能会更安全。

也许可以保证 B 的实例不能在没有 A 的实例的情况下生存,然后我的建议就不适用,但是我们在这里缺乏上下文来给出一个对此的明确建议。

Your code is quite correct (it works), but you can use the initialization list, like this:

C::C() :
  mA(new A),
  mB(new B(mA.get())
{
}

Which is even more correct and as safe.

If, for whatever reason, new A or new B throws, you'll have no leak.

If new A throws, then no memory is allocated, and the exception aborts your constructor as well. Nothing was constructed.

If new B throws, and the exception will still abort your constructor: mA will be destructed properly.

Of course, since an instance of B requires a pointer to an instance of A, the declaration order of the members matters.

The member declaration order is correct in your example, but if it was reversed, then your compiler would probably complain about mB beeing initialized before mA and the instantiation of mB would likely fail (since mA would not be constructed yet, thus calling mA.get() invokes undefined behavior).


I would also suggest that you use a shared_ptr<A> instead of a A* as a parameter for your B constructor (if it makes senses and if you can accept the little overhead). It would probably be safer.

Perhaps it is guaranteed that an instance of B cannot live without an instance of A and then my advice doesn't apply, but we're lacking of context here to give a definitive advice regarding this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文