mardi 29 mars 2016

How to deal with smart_ptr when class method returns reference

In my project i would like to make ever object I being organized with an smart_ptr. Now if a class method returns a pointer to itself, how can i deal with this using smart_pointers correctly?

class bar{
public:
    std::shared_ptr<bar> foo(){std::shared_ptr<bar>(this);}
};

int main()
{
    auto a = std::make_shared<bar>();
    auto b = a->foo();
    std::cout << a.use_count() << std::endl; //outputs 1 
}

Aucun commentaire:

Enregistrer un commentaire