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;
};
Frequently the Factory pattern is used for polymorphism: the Factory will return a specific concrete type of some abstract type. In this case it must return a pointer to a heap-allocated instance (else the return value will get sliced).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.