[英]Getting a boost::shared_ptr for this
I am making extensive use of boost:shared_ptr
in my code. 我在我的代码中广泛使用
boost:shared_ptr
。 In fact, most of the objects that are allocated on the heap are held by a shared_ptr
. 实际上,堆上分配的大多数对象都由
shared_ptr
持有。 Unfortunately this means that I can't pass this
into any function that takes a shared_ptr
. 不幸的是,这意味着我无法通过
this
成需要的任何功能shared_ptr
。 Consider this code: 考虑以下代码:
void bar(boost::shared_ptr<Foo> pFoo)
{
...
}
void Foo::someFunction()
{
bar(this);
}
There are two problems here. 这里有两个问题。 First, this won't compile because the T* constructor for
shared_ptr
is explicit. 首先,这不会编译,因为
shared_ptr
的T *构造函数是显式的。 Second, if I force it to build with bar(boost::shared_ptr<Foo>(this))
I will have created a second shared pointer to my object that will eventually lead to a double-delete. 其次,如果我强制它用
bar(boost::shared_ptr<Foo>(this))
构建bar(boost::shared_ptr<Foo>(this))
我将创建第二个指向我的对象的共享指针,最终将导致双删除。
This brings me to my question: Is there any standard pattern for getting a copy of the existing shared pointer you know exists from inside a method on one of those objects? 这让我想到了一个问题:是否有任何标准模式可以从其中一个对象的方法中获取您知道的现有共享指针的副本? Is using intrusive reference counting my only option here?
使用侵入式引用计数是我唯一的选择吗?
You can derive from enable_shared_from_this and then you can use "shared_from_this()" instead of "this" to spawn a shared pointer to your own self object. 您可以从enable_shared_from_this派生,然后您可以使用“ shared_from_this ()”而不是“this”来生成指向您自己的自身对象的共享指针。
Example in the link: 链接示例:
#include <boost/enable_shared_from_this.hpp>
class Y: public boost::enable_shared_from_this<Y>
{
public:
shared_ptr<Y> f()
{
return shared_from_this();
}
}
int main()
{
shared_ptr<Y> p(new Y);
shared_ptr<Y> q = p->f();
assert(p == q);
assert(!(p < q || q < p)); // p and q must share ownership
}
It's a good idea when spawning threads from a member function to boost::bind to a shared_from_this() instead of this. 从成员函数生成线程到boost :: bind到shared_from_this()而不是这个时,这是一个好主意。 It will ensure that the object is not released.
它将确保不释放对象。
Just use a raw pointer for your function parameter instead of the shared_ptr. 只需使用函数参数的原始指针而不是shared_ptr。 The purpose of a smart pointer is to control the lifetime of the object, but the object lifetime is already guaranteed by C++ scoping rules: it will exist for at least as long as the end of your function.
智能指针的目的是控制对象的生命周期,但C ++作用域规则已经保证了对象的生命周期:它至少与函数结束一样长。 That is, the calling code can't possibly delete the object before your function returns;
也就是说,调用代码不能在函数返回之前删除对象; thus the safety of a "dumb" pointer is guaranteed, as long as you don't try to delete the object inside your function.
因此,只要您不尝试删除函数内的对象,就可以保证“哑”指针的安全性。
The only time you need to pass a shared_ptr into a function is when you want to pass ownership of the object to the function, or want the function to make a copy of the pointer. 您需要将shared_ptr传递给函数的唯一时间是您希望将对象的所有权传递给函数,或者希望函数复制指针。
boost有这个用例的解决方案,请检查enable_shared_from_this
Are you really making more shared copies of pFoo inside bar? 你真的在酒吧内制作更多pFoo的共享副本吗? If you aren't doing anything crazy inside, just do this:
如果你内心没有做任何疯狂的事情,那就这样做:
void bar(Foo &foo)
{
// ...
}
With C++11 shared_ptr
and enable_shared_from_this
is now in the standard library. 使用C ++ 11
shared_ptr
和enable_shared_from_this
现在位于标准库中。 The latter is, as the name suggests, for this case exactly. 顾名思义,后者正好适用于这种情况。
http://en.cppreference.com/w/cpp/memory/shared_ptr http://en.cppreference.com/w/cpp/memory/shared_ptr
http://en.cppreference.com/w/cpp/memory/enable_shared_from_this http://en.cppreference.com/w/cpp/memory/enable_shared_from_this
Example bases on that in the links above: 示例基于以上链接中的示例:
struct Good: std::enable_shared_from_this<Good>{
std::shared_ptr<Good> getptr() {
return shared_from_this();
}
};
use: 使用:
std::shared_ptr<Good> gp1(new Good);
std::shared_ptr<Good> gp2 = gp1->getptr();
std::cout << "gp2.use_count() = " << gp2.use_count() << '\n';
The function accepting a pointer wants to do one of two behaviors: 接受指针的函数想要执行以下两种行为之一:
Edit: Oops I slightly misread the question, and I now see this answer is not exactly addressing the question. 编辑:哎呀我有点误读了这个问题,我现在看到这个答案并没有完全解决这个问题。 I'll leave it up anyway, in case this might be helpful for anyone working on similar code.
无论如何,我会把它留下来,以防这对任何从事类似代码工作的人都有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.