Wie kann ich halte Referenz in boost :: shared_ptr mit boost :: bind ohne Definition der expliziten Funktion?

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

  •  20-09-2019
  •  | 
  •  

Frage

Ich mag Objekt zu halten Referenz, damit es nicht in bind Funktion gelöscht wird, aber ohne Hilfsfunktion verwendet wird.

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

}

Gibt es eine Möglichkeit holdReference Funktion in Position zu erklären? Wie bei Lambda-Ausdrücke oder etw? (Ohne diese fiesen holdReference-Funktion, die außerhalb des Geltungsbereichs der Spaß Funktion deklariert werden müssen) Ich hatte einige Versuche, aber nicht von ihnen zusammengestellt:)

Ok, hier ist detaillierteres Beispiel:

#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
War es hilfreich?

Lösung

Sie können für die „gemeinsame Verantwortung“ Konstruktor suchen werden, dies ermöglicht ref Zählen einen inneren Zeiger.

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

Andere Tipps

Ich bin ein bisschen verwirrt von dem, was Sie versuchen, hier zu tun.

Sie Spaß () soll Rückkehr boost::shared_ptr<int> oder boost::shared_ptr<Int> ???

Ich glaube nicht, dass Sie eine shared_ptr<int> erstellen mögen, dass ein gemeinsamer Zeiger um einen Rohzeiger ist, die direkt vom Int Objekt gehörte, als das Objekt Int die _int löscht, wenn es Spielraum geht (auch wenn im Beispiel tat es nicht ‚neu‘ es!).

Sie müssen sich mit einem klaren Eigentum / Verantwortung Modell zu entwickeln.

Vielleicht können Sie bieten eine andere, realistischere, Beispiel dafür, was Sie erreichen wollen?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top