std :: enable_shared_from_this;公共与私人 [英] std::enable_shared_from_this; public vs private

查看:155
本文介绍了std :: enable_shared_from_this;公共与私人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用shared_ptr和enable_shared_from_this玩了一段时间,而遇到了我不太了解的东西。

I was playing around for a bit using the shared_ptr's and enable_shared_from_this, while I run into something I don't really understand.

在我第一次尝试时,我构造了一些东西像这样:

In my first attempt I constructed something like this:

class shared_test : std::enable_shared_from_this<shared_test> {
public:
    void print(bool recursive) {
        if (recursive) {
            shared_from_this()->print(false);
        }

        std::cout << "printing" << std::endl;
    }
};

请注意,该类是私自扩展std :: enable_shared_from_this的。这显然有很大的不同,因为执行这样的事情:

Please note that this class is extending std::enable_shared_from_this privately. This apparently makes a lot of difference because executing a something like this:

int main() {
    auto t(std::make_shared<shared_test>());
    t->print(true);
    return 0;
}

抛出bad_weak_ptr异常。好像我将类定义从std :: enable_shared_from_this公开更改为固有的一样,这只是在查找。

throws an bad_weak_ptr exception. Where as if I change the class definition to inherent publicly from std::enable_shared_from_this this runs just find.

为什么,我在这里想念什么?而且没有办法使它适用于私有继承,因为shared_test类的外部世界不需要知道它是否已从中启用共享……(至少,如果您问我,还是我会再次错过某些东西?)

Why is that, what do I miss here? And isn't there a way to make it work for private inheritance, since the 'outside world' of the shared_test class does not need to know that it is enabling shared from this... (at least, not if you ask me, or do I miss something again?)

推荐答案


为什么呢,我在这里想念什么?

Why is that, what do I miss here?

要使 shared_from_this 起作用, enable_shared_from_this 必须知道保存该类的 shared_ptr 。在您的STL实现中,它是 weak_ptr ,也可以通过其他实现。私有继承时,将无法从类外部访问基类的属性。实际上,甚至无法了解您是从那里继承来的。因此, make_shared 会生成常规的shared_ptr初始化,而无需在 enable_shared_from_this 中设置适当的字段。

To make shared_from_this work enable_shared_from_this has to know about shared_ptr which holds the class. In your STL implementation it is weak_ptr, through other implementations are possible. When you inherit privately, then it is not possible to access base class's properties from the outside of your class. Actually it is not even possible to understand that you have inherited from. So make_shared generates usual shared_ptr initialization without setting proper fields in enable_shared_from_this.

不是从 make_shared 引发异常,而是从 shared_from_this 引发异常,因为 enable_shared_from_this 未正确初始化。

Exception is thrown not from make_shared but form shared_from_this because enable_shared_from_this was not initialized properly.


并且没有办法使其可用于私有继承,因为外部shared_test类的世界不需要知道它是否正在从此启用共享...

And isn't there a way to make it work for private inheritance, since the 'outside world' of the shared_test class does not need to know that it is enabling shared from this...

否。外界必须知道该对象与shared_ptr有特殊关系才能正常使用。

No. Outside world has to know that the object has special relations with shared_ptr to work properly with it.

这篇关于std :: enable_shared_from_this;公共与私人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆