How to conditionally create different child classes?

For example, in the main function, I want to get user input. Depending on the input, I will create either a Rectangle or Circle , which are child classes of Object . If there is no input (or unknown), then I will just create a shared object.

 class Object { public: Object(); void Draw(); private: .... }; class Rectangle:public Object { public: Rectangle(); .... //it might have some additional functions private: .... }; class Circle:public Object { public: Circle(); .... //it might have some additional functions private: .... }; 

The main function:

 string objType; getline(cin, objType); if (!objType.compare("Rectangle")) Rectangle obj; else if (!objType.compare("Circle")) Circle obj; else Object obj; obj.Draw(); 

Of course, the code above will not work, because I cannot create an instance of the object inside the If statement. So I tried something like this.

 Object obj; if (!objType.compare("Rectangle")) obj = Rectangle(); else if (!objType.compare("Circle")) obj = Circle(); obj.Draw(); 

This code will compile, but it will not do what I want. For some reason, the object was not initiated in the way the child class should be executed (for example, I set some member variables of the object, in particular the vector, otherwise in the child classes). However, when I set a breakpoint in the constructor of the Child class, it passed through it.

So, how do I put instance objects as my child classes in some if-statements ??

+6
source share
1 answer

You can create automatic objects in if , but they will be destroyed at the end of the area in which they are created, so they do not work for this problem.

The reason you cannot do obj = Rectangle() is because of slicing .

You must have a pointer to an Object . Pointers to base objects can also point to instances of child objects. Then you can dynamically create an object inside the if with new (objects created with new ignore the scope and are destroyed only when the delete pointer is called on them), and then delete done:

 Object* obj = NULL; // obj doesn't point to anything yet string objType; getline(cin, objType); if (objType == "Rectangle") obj = new Rectangle; // make obj point to a dynamic Rectangle else if (objType == "Circle") obj = new Circle; // make obj point to a dynamic Circle else obj = new Object; // make obj point to a dynamic Object obj->Draw(); // draw whatever shape obj really points to delete obj; // deallocate dynamic object 

Alternatively, you can use smart pointers, and then you don't have to worry about manually freeing the object:

 std::unique_ptr<Object> obj(NULL); // obj doesn't point to anything yet string objType; getline(cin, objType); if (objType == "Rectangle") obj.reset(new Rectangle); // make obj point to a dynamic Rectangle else if (objType == "Circle") obj.reset(new Circle); // make obj point to a dynamic Circle else obj.reset(new Object); // make obj point to a dynamic Object obj->Draw(); // draw whatever shape obj really points to // the unique_ptr takes care of delete'ing the object for us // when it goes out of scope 
+10
source

Source: https://habr.com/ru/post/909419/


All Articles