lundi 19 mars 2018

Returning unique_ptr or just moving object?

I have a factory that is creating objects, but I'm unsure what the best way is to return these objects. I have the option to return a unique_ptr<Trigger>, or I could just return Trigger that is handled by a Move constructor. What is better practice? My best guess is Move because you are guaranteed an object.

class TriggerFactory
{
public:
    TriggerFactory();
    ~TriggerFactory();

    Trigger createMyTrigger() const; // Trigger contains a move constructor
};

vs

class TriggerFactory
{
public:
    TriggerFactory();
    ~TriggerFactory();

    unique_ptr<Trigger> createMyTrigger() const;
};

Aucun commentaire:

Enregistrer un commentaire