Come posso tenere riferimento nel boost :: shared_ptr usare il boost :: bind senza definizione di funzione esplicita?

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

  •  20-09-2019
  •  | 
  •  

Domanda

Voglio tenere riferimento ad oggetto in modo che non venga cancellata in funzione bind, ma senza usare funzione di supporto.

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

}

C'è un modo per dichiarare la funzione holdReference in atto? Come con le espressioni lambda o sth? (Senza l'utilizzo di questa funzione holdReference brutta, che devono essere dichiarati al di fuori del campo di applicazione della funzione di divertimento) Ho avuto alcuni tentativi, ma non di loro compilato:)

Ok, qui è esempio più dettagliato:

#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
È stato utile?

Soluzione

Si può essere alla ricerca per il costruttore "proprietà condivisa", questo permette ref contare un puntatore interno.

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

Altri suggerimenti

Sono un po 'confuso da ciò che si sta cercando di fare qui.

divertimento () deve restituire boost::shared_ptr<int> o boost::shared_ptr<Int> ???

Non credo che si desidera creare un shared_ptr<int> che è un puntatore condiviso attorno a un puntatore prima che è direttamente posseduto dall'oggetto Int, come l'oggetto Int eliminerà il _int ogni volta che si va fuori del campo di applicazione (anche se nell'esempio lo ha fatto non e 'nuova' it!).

E 'necessario trovare una chiara proprietà / modello di responsabilità.

Forse si può fornire un diverso, più realistico, esempio di ciò che si sta cercando di raggiungere?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top