Comment puis-je tenir en référence boost :: shared_ptr utilisant boost :: bind sans définition de fonction explicite?

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

  •  20-09-2019
  •  | 
  •  

Question

Je veux tenir référence à l'objet afin qu'il ne soit pas supprimé en fonction de liaison, mais sans utiliser la fonction d'aide.

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

}

Est-il possible de déclarer la fonction holdReference en place? Comme avec les expressions lambda ou qqch? (Sans utiliser cette fonction de holdReference méchant, qui doivent être déclarés en dehors de la portée de la fonction fun) J'ai eu quelques essais, mais non d'entre eux compilé:)

Ok, voici exemple plus détaillé:

#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
Était-ce utile?

La solution

Vous cherchez peut-être le constructeur « propriété partagée », ce qui permet ref compter un pointeur intérieur.

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

Autres conseils

Je suis un peu confus par ce que vous essayez de faire ici.

est amusant () censé revenir boost::shared_ptr<int> ou boost::shared_ptr<Int> ???

Je ne pense pas que vous voulez créer un shared_ptr<int> qui est un pointeur partagé autour d'un pointeur brut qui appartient directement par l'objet Int, comme l'objet Int supprimera le _int chaque fois qu'il est hors de portée (même si dans l'exemple, il n'a pas « nouveau » le!).

Vous devez trouver un modèle de propriété clair / responsabilité.

Peut-être que vous pouvez fournir un autre, plus réaliste, par exemple de ce que vous essayez d'atteindre?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top