在没有显式函数定义的情况下,如何使用 boost::bind 在 boost::shared_ptr 中保存引用?
我想保留对对象的引用,这样它就不会在绑定函数中被删除,但不使用辅助函数。
struct Int
{
int *_int;
~Int(){ delete _int; }
};
void holdReference(boost::shared_ptr<Int>, int*) {} // helper
boost::shared_ptr<int> fun()
{
boost::shared_ptr<Int> a ( new Int );
// I get 'a' from some please else, and want to convert it
a->_int = new int;
return boost::shared<int>( a->_int, boost::bind(&holdReference, a, _1) );
}
有没有办法在适当的位置声明holdReference函数?就像 lambda 表达式或者什么? (不使用这个讨厌的holdReference函数,它必须在fun函数的范围之外声明) 我尝试了几次,但都没有编译:)
好的,这里是更详细的示例:
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
// the case looks more or less like this
// this class is in some dll an I don't want to use this class all over my project
// and also avoid coppying the buffer
class String_that_I_dont_have
{
char * _data; // this is initialized in 3rd party, and released by their shared pointer
public:
char * data() { return _data; }
};
// this function I created just to hold reference to String_that_I_dont_have class
// so it doesn't get deleted, I want to get rid of this
void holdReferenceTo3rdPartyStringSharedPtr( boost::shared_ptr<String_that_I_dont_have>, char *) {}
// so I want to use shared pointer to char which I use quite often
boost::shared_ptr<char> convert_function( boost::shared_ptr<String_that_I_dont_have> other)
// 3rd party is using their own shared pointers,
// not the boost's ones, but for the sake of the example ...
{
return boost::shared_ptr<char>(
other->data(),
boost::bind(
/* some in place here instead of holdReference... */
&holdReferenceTo3rdPartyStringSharedPtr ,
other,
_1
)
);
}
int main(int, char*[]) { /* it compiles now */ }
// I'm just looking for more elegant solution, for declaring the function in place
I want to hold reference to object so it doesn't get deleted in bind function, but without using helper function.
struct Int
{
int *_int;
~Int(){ delete _int; }
};
void holdReference(boost::shared_ptr<Int>, int*) {} // helper
boost::shared_ptr<int> fun()
{
boost::shared_ptr<Int> a ( new Int );
// I get 'a' from some please else, and want to convert it
a->_int = new int;
return boost::shared<int>( a->_int, boost::bind(&holdReference, a, _1) );
}
Is there a way to declare holdReference function in place? Like with lambda expressions or sth? (without using this nasty holdReference function, that have to be declared outside the scope of fun function)
I had few tries but non of them compiled :)
Ok, here is more detailed example:
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
// the case looks more or less like this
// this class is in some dll an I don't want to use this class all over my project
// and also avoid coppying the buffer
class String_that_I_dont_have
{
char * _data; // this is initialized in 3rd party, and released by their shared pointer
public:
char * data() { return _data; }
};
// this function I created just to hold reference to String_that_I_dont_have class
// so it doesn't get deleted, I want to get rid of this
void holdReferenceTo3rdPartyStringSharedPtr( boost::shared_ptr<String_that_I_dont_have>, char *) {}
// so I want to use shared pointer to char which I use quite often
boost::shared_ptr<char> convert_function( boost::shared_ptr<String_that_I_dont_have> other)
// 3rd party is using their own shared pointers,
// not the boost's ones, but for the sake of the example ...
{
return boost::shared_ptr<char>(
other->data(),
boost::bind(
/* some in place here instead of holdReference... */
&holdReferenceTo3rdPartyStringSharedPtr ,
other,
_1
)
);
}
int main(int, char*[]) { /* it compiles now */ }
// I'm just looking for more elegant solution, for declaring the function in place
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对你在这里尝试做的事情有点困惑。
fun() 应该返回
boost::shared_ptr
还是boost::shared_ptr
???我认为您不想创建一个
shared_ptr
它是围绕 Int 对象直接拥有的原始指针的共享指针,因为 Int 对象每当它删除 _int 时都会删除 _int超出范围(即使在示例中它没有“新”它!)。您需要提出一个清晰的所有权/责任模型。
也许您可以提供一个不同的、更现实的例子来说明您想要实现的目标?
I am a bit confused by what you are trying to do here.
Is fun() supposed to return
boost::shared_ptr<int>
orboost::shared_ptr<Int>
???I don't think that you want to create a
shared_ptr<int>
that is a shared pointer around a raw pointer that is directly owned by the Int object, as the Int object will delete the _int whenever it goes out of scope (even though in the example it did not 'new' it!).You need to come up with a clear ownership/responsibility model.
Perhaps you can provide a different, more realistic, example of what you are trying to achieve?
您可能正在寻找“共享所有权”构造函数,这允许对内部指针进行引用计数。
You may be looking for the "shared ownership" constructor, this allows ref counting an interior pointer.