Store data of multiple types

Dear forum,

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.

Is there a solution to this problem?

Regards,
PascalM123
You can get the type of something with http://en.wikipedia.org/wiki/Typeid
Or you could make a virtual function that returns the type, or a number so you can identify what it is http://cplusplus.com/forum/beginner/27218/

Otherwise you could use void pointers if you want to be able to store anything...
Last edited on
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?
Last edited on
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.
Don't go the typeid route.

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?
PascalM123 wrote:
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?


:)
+1 firedraco, moorecm

1
2
3
4
5
6
7
8
9
10
std::list<Shape*> shapes;

void add_shape(Shape* s)
{
  shapes.push_back(s);
}

//...

myscene.add_shape( new SquareShape( properties, of, the, square ) );


Just remember to delete the shapes when you remove them from the list or you'll have memory leaks. Also make sure Shape has a virtual destructor.
Indeed, that did the trick!

Thank you all for your replies, you all helped me a lot!

Greetings,
PascalM123
Topic archived. No new replies allowed.