我们什么时候应该使用std :: enable_shared_from_this [英] When should we use std::enable_shared_from_this
问题描述
我只是知道 std :: enable_shared_from_this
表单此链接。
但是阅读下面的代码后,我不知道何时使用它。
I just knew std::enable_shared_from_this
form this link.
But after reading the code below, I don't know when to use it.
try {
Good not_so_good;
std::shared_ptr<Good> gp1 = not_so_good.getptr();
} catch(std::bad_weak_ptr& e) {
// undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17)
std::cout << e.what() << '\n';
}
上面的代码不太好,因为没有现有的 shared_ptr
,然后调用 getptr()
。因此,好事应该是:
The code above is "not so good" because there is no existing shared_ptr
before calling getptr()
. So the good thing should be:
std::shared_ptr<Good> gp1 = std::make_shared<Good>(); // having a shared_ptr at the beginning
std::shared_ptr<Good> gp2 = gp1->getptr();
但是,如果我已经有 shared_ptr
对象,为什么我不只是这样简单地编码: std :: shared_ptr< Good> gp2 = gp1;
,这意味着我根本不需要 std :: enable_shared_from_this
。
However, if I have already had a shared_ptr
object, why don't I just simply code like this: std::shared_ptr<Good> gp2 = gp1;
, meaning that I don't need std::enable_shared_from_this
at all.
我认为,使用 std :: enable_shared_from_this
可以确保多个 shared_ptr
对象具有相同的控制块,因此我们可以避免重复删除问题。但是,如果我必须提醒自己在开始时创建 shared_ptr
,为什么我不只是提醒自己使用 shared_ptr
对象创建一个新对象,而不使用原始指针?
In my opinion, using std::enable_shared_from_this
is to make sure that more than one shared_ptr
objects have the same control block so that we can avoid the double-delete problem. But if I must remind myself to create a shared_ptr
at the beginning, why don't I just remind myself to use shared_ptr
object to create a new one, instead of using raw pointer?
推荐答案
关于何时 std的提示:: enable_shared_from_this< T>
很有用,因为它的名称是:根据某些请求产生对象时,可能有必要返回指向对象本身的指针。如果结果应为 std :: shared_ptr< T>
,则有必要从通常没有的成员函数中返回此类指针。 std :: shared_ptr< T>
可访问。
The hint about when std::enable_shared_from_this<T>
is useful is in its name: when yielding objects based on some requests it may be necessary to return a pointer to an object itself. If the result should be a std::shared_ptr<T>
it becomes necessary to return such a pointer from within a member function where there is generally no std::shared_ptr<T>
accessible.
源自 std :: enable_shared_from_this< T>
提供了一种获取 std :: shared_ptr< T>
的方法,仅给出类型为 T $ c $的指针c>。但是,这样做确实假定已通过
std :: shared_ptr< T>
管理对象,并且如果将对象分配在堆栈上,则会产生混乱:
Having derived from std::enable_shared_from_this<T>
provides a way to get hold of a std::shared_ptr<T>
given just a pointer of type T
. Doing so does, however, assume that the object is already managed via a std::shared_ptr<T>
and it would create mayhem if the object is allocated on the stack:
struct S: std::enable_shared_from_this<S> {
std::shared_ptr<S> get_object() {
return this->shared_from_this();
};
}
int main() {
std::shared_ptr<S> ptr1 = std::make_shared<S>();
std::shared_ptr<S> ptr2 = ptr1->get_object();
// ...
}
在现实情况下,可能在某些情况下返回当前对象的 std :: shared_ptr< T>
。
In a realistic scenario there is probably some condition under which a std::shared_ptr<T>
to the current object is returned.
这篇关于我们什么时候应该使用std :: enable_shared_from_this的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!