enable_shared_from_this (c++0x): what am I doing wrong?

前端 未结 2 1848
灰色年华
灰色年华 2020-12-11 03:45

I\'m just toying around with the smart pointers in the upcoming new c++ standard. However I fail to grasp the usage of the shared_from_this function. Here is what I have:

2条回答
  •  轮回少年
    2020-12-11 04:14

    It is a precondition of using shared_from_this that there must exist at least one shared_ptr which owns the object in question. This means that you can only use shared_from_this to retrieve a shared_ptr that owns an object to which you have a reference or pointer, you cannot use it to find out if such an object is owned by a shared_ptr.

    You need to rework your design so that either you are guaranteed that any such object is being managed by a shared_ptr or that you don't ever need to know or finally (and least desirably) you create some other way of managing this knowledge.

提交回复
热议问题