mercredi 10 juillet 2019

Improving safety of Clone pattern

If one wants to implement Clone pattern in C++, he might not be sure about safety, because derived class may forget to override it:

struct A {
    virtual A* Clone() const {
        return new A(*this);
    }
}

struct B : A {
    int value;
};

int main() {
   B b;
   // oops
   auto b_clone = b.Clone();
}

What are the possible ways to improve Clone pattern in C++ in this regard?

Aucun commentaire:

Enregistrer un commentaire