¿Cómo puedo sostener referencia en impulso :: shared_ptr usar boost :: bind sin definición de función explícita?

StackOverflow https://stackoverflow.com/questions/2453048

  •  20-09-2019
  •  | 
  •  

Pregunta

Quiero abrazarte referencia al objeto para que no se borran de la función de enlace, pero sin utilizar la función de ayudante.

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

}

¿Hay una forma de declarar la función holdReference en su lugar? Al igual que con las expresiones lambda o algo? (Sin necesidad de utilizar esta función holdReference desagradable, que tienen que ser declarada fuera del ámbito de la función de la diversión) Tenía algunos intentos pero no de ellos compilado:)

Ok, aquí es ejemplo más detallado:

#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
¿Fue útil?

Solución

Es posible que se busca el constructor "propiedad compartida", esto permite ref contando un puntero interior.

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

Otros consejos

Estoy un poco confundido por lo que está tratando de hacer aquí.

es divertido () supone volver boost::shared_ptr<int> o boost::shared_ptr<Int> ???

No creo que desea crear un shared_ptr<int> que es un puntero compartida en torno a un puntero en bruto que es propiedad directamente por el objeto Int, Int como el objeto se eliminará la _int cada vez que se sale del ámbito (aunque en el ejemplo no lo hizo 'nuevo' que!).

Es necesario llegar a un modelo claro de propiedad / responsabilidad.

Tal vez se puede proporcionar una diferente, más realista, ejemplo de lo que está tratando de lograr?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top