C++ 在没有显式函数定义的情况下,如何使用boost::bind在boost::shared_ptr中保存引用?

C++ 在没有显式函数定义的情况下,如何使用boost::bind在boost::shared_ptr中保存引用?,c++,boost,bind,shared-ptr,C++,Boost,Bind,Shared Ptr,我想保留对对象的引用,这样它就不会在绑定函数中被删除,而不会使用helper函数 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 );

我想保留对对象的引用,这样它就不会在绑定函数中被删除,而不会使用helper函数

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) );

}
struct Int
{
int*_int;
~Int(){delete}Int;}
};
void holdReference(boost::shared_ptr,int*){}//helper
boost::共享的乐趣()
{
boost::shared_ptr a(新Int);
//我从其他人那里得到了“a”,并想转换它
a->_int=新int;
返回boost::shared(a->int,boost::bind(&holdReference,a,_1));
}
有没有办法在适当的位置声明holdReference函数?比如说lambda表达式之类的?(不使用此引用函数,必须在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
#包括
#包括
//这个案子差不多是这样的
//这个类在某个dll中,我不想在我的项目中使用这个类
//同时也要避免对缓冲区进行铜化
我没有的类字符串
{
char*_data;//这在第三方中初始化,并由其共享指针释放
公众:
char*data(){return\u data;}
};
//我创建这个函数只是为了保存对字符串的引用,而这个字符串没有类
//所以它不会被删除,我想去掉这个
void holdReferenceTo3rdPartyStringSharedPtr(boost::shared_ptr,char*){}
//所以我想使用我经常使用的指向char的共享指针
boost::shared_ptr convert_函数(boost::shared_ptr other)
//第三方使用自己的共享指针,
//不是助推,但为了这个例子。。。
{
返回提升::共享的ptr(
其他->数据(),
boost::bind(
/*这里有一些,而不是参考*/
&holdReferenceTo3rdPartyStringSharedPtr,
其他,,
_1
)
);
}
int main(int,char*[]){/*它现在编译*/}
//我只是在寻找更优雅的解决方案,在适当的位置声明函数

我对您在这里试图做的事情感到有点困惑

fun()应该返回
boost::shared_ptr
还是
boost::shared_ptr

我认为您不想创建一个
共享的\u ptr
,它是一个围绕Int对象直接拥有的原始指针的共享指针,因为Int对象在它超出范围时会删除它(即使在本例中它没有“新建”它!)

你需要提出一个明确的所有权/责任模式


也许您可以提供一个不同的、更现实的示例来说明您试图实现的目标?

您可能正在寻找“共享所有权”构造函数,这允许对内部指针进行ref计数

struct Int
{
   int *_int;
   ~Int(){ delete _int; }
};

boost::shared_ptr<int> fun()
{
   boost::shared_ptr<Int> a (new Int);
   a->_int = new int;

   // refcount on the 'a' instance but expose the interior _int pointer
   return boost::shared_ptr<int>(a, a->_int);
}
struct Int
{
int*_int;
~Int(){delete}Int;}
};
boost::共享的乐趣()
{
boost::shared_ptr a(新Int);
a->_int=新int;
//“a”实例上的refcount,但暴露内部的int指针
返回boost::共享的ptr(a,a->int);
}

这是无法编译的。请发布您的实际代码,并解释您试图实现的目标