Assuming you want to store objects that do not inherit a common class, the simplest way that comes to mind is to use boost::any
.
You still have to be sure about what's the type of each object stored at each index (i.e. to be able to perform the correct boost::any_cast
).
You should do that rather than storing pointers to void
. This is the closest as it gets to the semantically correct way of storing "something you know the type but the compiler doesn't", implying a cast when retrieving the value.
While both (any
and a void
pointer) will work the same way (pointer aside), if you cast to the wrong type, any
will throw an bad_any_cast
exception (IIRC), while with the void
pointer, you'll get undefined behavior. A simple try on coliru yielded a segfault.