I need some way to declare a linked list (or vector) where each element may be of any class.
Unfortunately this seems to be impossible with the standart containers, because all elements in a std::list or std::vector have to be of the same type.
My basic idea to achive this was using polymorphism:
1 2 3 4 5 6 7 8 9 10 11 12 13
class ElementBase {}; //Empty base class
class Scalar_Float : public ElementBase {
public:
float val;
};
class Scalar_Int : public ElementBase {
public:
int val;
};
std::list<ElementBase*> LL;
Like this i can add elements of arbitrary classes to the linked list, but i just can't figure out how to read the contents of the linked list.
I could add a pure virtual function to the ElementBase class to retrieve the value(s) stored in the derived classes, but i can't provide a type for the return value, because the type of "val" is always different.