vendredi 11 mars 2016

What the difference between Prototype design pattern and copy constructor in C++

I am trying to understand when I should use Prototype design pattern. Here is example of Prototype as I understand it:

class Prototype
{
public:
    virtual Prototype* clone() = 0;
...
};

class ConcretePrototype
{
public:
    Prototype* clone() override { ... }
};

// Usage:
ConcretePrototype proto;
auto protPtr = proto.clone();

Where is a question: Why this better than:

class Obj
{
public:
    Obj();

    Obj(const Obj&);
    Obj& operator = (const Obj& other);
};

Obj o;
Obj o2 = o;

So when I actually should use Prototype?

Aucun commentaire:

Enregistrer un commentaire