mardi 19 mai 2020

How to create an object creation function that will be called by the name associated with it?

I have a class inheritance hierarchy: Figure -> Circle, Point, Line, Rectangle.

I need to create a function to create a Circle graphical object from a given hierarchy, by the name associated with it. The function should return unique_ptr to the object. The parameters of the function are the name of the object and its characteristics (x, y, radius). When adding new classes to the hierarchy, the function should not be changed.

Tell me, how can I implement this function? I do not understand

For example ?? :

unique_ptr<Figure> CreateFigure(const std::string& name) {
     if (name == "circle")
        return make_unique<Circle>();
     if (name == "line")
        return make_unique<Line>()

Aucun commentaire:

Enregistrer un commentaire