I am interested if there is a better way to dynamically create objects from a base class pointer. Here's an example of my situation:
// I have an array of base class pointers of type square. Box is derived from square. I first create a new box object with params with new using a square pointer.
32 square * sqr_arr[10];
33
34 square * a_square;
35
36 a_square = new box(arr, 4, 5, 7);
37
38 box * temp = dynamic_cast<box *>(a_square);
39
40 sqr_arr[0] = new box(*temp);
41
42 sqr_arr[0]->display();
My Trouble is that in my actual program I am constantly moving the pointers in the array around based on A-Z sorting, but I am having difficulty with the fact that each time I need to know which derived class I will be creating instead of the pointer "knowing" what it is pointing to and creating an object of that type.
Let's say in the code above I wanted to move sqr_arr[0] to sqr_arr[1]. Is there anyway sqr_arr[1] would be able to just make a copy with the correct use of new. In my actual program, for example, sqr_arr could create many different objects so my only solution is to iterate through with dynamic_cast of until it returns an acceptable pointer to the derive * temp.. Is this a good solution or could it be worked out in a different way?