vendredi 14 juillet 2017

Is this a misuse of unique_ptr?

I'm making the shift towards smart pointers, and I'm trying to make sure I use them properly. There are plenty of questions that cover when to use each one, but I wasn't able to find a question specifically about getters.

I have a class that owns a pointer, and I want other classes to be able to access that pointer (Refactoring legacy code in steps). I wanted to give the class a unique_ptr because it will only own that object, but they can't be copied. Should I be returning a reference to the unique_ptr, or just using a shared_ptr?

class B
{
 public:
    doAction() {};
};

class A
{
 private:
    std::unqiue_ptr<B> pointer;

 public:
    std::unique_ptr<B>& GetPointer()
    {
        return pointer;
    }

};

a.GetPointer()->doAction();

Aucun commentaire:

Enregistrer un commentaire