enable_shared_from_this - empty internal weak pointer?

前端 未结 3 2024
鱼传尺愫
鱼传尺愫 2020-12-31 01:15

I\'m using enable_shared_from_this and then inherit from Base. When trying to use shared_from_this() in Derived

3条回答
  •  我在风中等你
    2020-12-31 01:46

    You cannot call shared_from_this() in the object's constructor. shared_from_this() requires that the object is owned by at least one shared_ptr. An object cannot be owned by a shared_ptr before it is constructed.

    I would guess that the internal weak pointer is set when a shared_ptr takes ownership of the object for the first time. Before that point, there is no reference count struct that the weak pointer can reference.

提交回复
热议问题