构造函数中指向 this 的弱指针

发布于 2024-10-10 12:17:22 字数 291 浏览 6 评论 0原文

我知道不可能通过从类的构造函数调用shared_from_this()来获取shared_ptr,因为对象尚未构造。但是是否可以从构造函数中获取对象的weak_ptr?一些讨论“weak_from_raw()”方法的 boost 论坛帖子表明这可能是可能的。

编辑:Boost表单讨论weak_from_raw http://lists.boost.org/boost-users/2010/ 08/61541.php

I understand it is not possible to obtain a shared_ptr by calling shared_from_this() from the constructor of a class, as the object is not yet constructed. Is it possible however to obtain a weak_ptr to the object from the constructor? Some boost forum posts discussing a "weak_from_raw()" method suggests that it may be possible.

Edit: Boost form discussing weak_from_raw http://lists.boost.org/boost-users/2010/08/61541.php

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

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

发布评论

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

评论(2

尬尬 2024-10-17 12:17:22

我认为您引用的是这个。这似乎没有合并到增强版本中(这可能是错误的)。

来自 boost 文档

常见问题

问: 对象能否在其构造函数中为自身创建一个weak_ptr?

A. 不可以。weak_ptr 只能从shared_ptr 创建,并且在对象构造时,尚不存在该对象的shared_ptr。即使您可以为此创建一个临时的shared_ptr,它也会在构造函数末尾超出范围,并且所有weak_ptr实例将立即过期。

解决方案是将构造函数设为私有,并提供一个返回shared_ptr的工厂函数:

class X
{
private:

    X();

public:

    static shared_ptr<X> create()
    {
        shared_ptr<X> px(new X);
        // create weak pointers from px here
        return px;
    }
};

I think what you're referencing is this. Which seems not to have been merged to the boost release (may be wrong about that).

From the boost docs:

Frequently Asked Questions

Q. Can an object create a weak_ptr to itself in its constructor?

A. No. A weak_ptr can only be created from a shared_ptr, and at object construction time no shared_ptr to the object exists yet. Even if you could create a temporary shared_ptr to this, it would go out of scope at the end of the constructor, and all weak_ptr instances would instantly expire.

The solution is to make the constructor private, and supply a factory function that returns a shared_ptr:

class X
{
private:

    X();

public:

    static shared_ptr<X> create()
    {
        shared_ptr<X> px(new X);
        // create weak pointers from px here
        return px;
    }
};
终难愈 2024-10-17 12:17:22

请参阅:http://boost.org/doc/libs/1_42_0 /libs/smart_ptr/sp_techniques.html#in_constructor

问题不在于未构造对象。问题是shared_ptr尚未构建。如果您要做的只是创建一个shared_ptr并将其发送到某个地方,那么一切都会好起来的。当您尝试创建一个shared_ptr来包含您刚刚创建的对象时。没有办法将两者联系起来,因此你有一个大问题。

Shared_from_this 的工作方式是,它希望您在调用shared_from_this 来访问该对象之前将该对象设置为shared_ptr。由于您还没有这样做,因为对象的构造函数尚未完成,因此shared_ptr没有连接到它,所以您无法调用shared_from_this。

您对weak_ptr 也会遇到完全相同的问题。

所以,换句话说,这个荒谬的构造是可行的:

struct absurd
{
  absurd()
  {
    register(shared_ptr<absurd>(this));
  }
};

...
new absurd; // better not assign to a shared_ptr!!!

但你真的不想这样做。

See: http://boost.org/doc/libs/1_42_0/libs/smart_ptr/sp_techniques.html#in_constructor

The issue isn't that the object isn't constructed. The issue is that the shared_ptr hasn't been constructed. If all you were to do was to create a shared_ptr and send it off somewhere everything would be fine. It's when you try to create a shared_ptr to contain the object you just created. There's no way to connect the two and thus you have a big problem.

The way that shared_from_this works is that it expects you to put the object is a shared_ptr before ever calling shared_from_this to gain access to it. Since you haven't yet done so, since the object's constructor hasn't finished and thus the shared_ptr is not connected to it, you can't call shared_from_this.

You'd have exactly the same issue with the weak_ptr.

So, in other words, this absurd construct would work:

struct absurd
{
  absurd()
  {
    register(shared_ptr<absurd>(this));
  }
};

...
new absurd; // better not assign to a shared_ptr!!!

But you really don't want to do this.

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