I have a question concerning the storage of multiple types of data. I have an abstract class Shape and multiple concrete classes who derive from that Shape class (like spheres etc). I also have a Scene class which is made to hold up all the concrete shapes in order to check collision and in order to draw or update position etc.
The problem is that I do not know how to store these classes in my Scene class. If I create a new class and I want to add that class to the Scene, I could make a function like add_actor or add_shape, but because I don't know if it is a Sphere or a Square, I have no idea hoe to identify the shape. The same problem goes for storing the data. If I make a list for example, I have to give a type, but there are multiple types that could be to the scene.
When I add a function to the Scene class like add_shape, you have to give a parameter like Shape new_shape, but the shape can be sphere_shape, square_shape etc.etc, so I do not have a single type for that function. The same problem goes for storing the shapes; I do not have a single type that has to be stored.
I don't think I need to know what type the shape is (at least, for now), as long as it is stored in the scene class and I can call the functions of these shapes. If I want to know what type it is, I can add parameters or functions in order to get the type.
The save method of each Shape should store a shape id, then their specific details to the stream. Each Shape should also be able to construct itself from what it wrote onto the stream.
The restore should be done with a Shape Factory which reads the id and constructs the specific kind of shape from the stream.
I do not know if I need a save method. The problem is the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class Shape {
//class members
};
class SphereShape : public Shape {
//class members
};
class SquareShape : public Shape {
//class members
};
class Scene {
std::list< ? > shapes;
void add_shape( ? );
};
I do not know what to put on the questionmarks. I want to add mulitple types of shapes (like in this code-part SphereShape and SquareShape), but how do I do that?
I do not know what to put on the questionmarks. I want to add mulitple types of shapes (like in this code-part SphereShape and SquareShape), but how do I do that?
firedraco wrote:
Sounds like you could use a vector of Shape* or something similar...but why do you need to know the type of shape it is?