Arrays of multiple data types

Started by
5 comments, last by djones 21 years, 3 months ago
Anyone got any ideas on how you would created an array or linked list that could contain any data type as an element in the same array. i.e. class bob { ... }; int a=2; float b=1.02; bob c; CoolArr[0]=a; CoolArr[1]=b; CoolArr[2]=c; I have managed to work around not needing such an array but it would be damn usefull if I could get it to work.
Advertisement
Well you could set it up manually to accept certain types and then overload the add functions... something like:


  enum EntryType{   FLOAT_DATA,   DOUBLE_DATA,   SHORT_DATA,   LONG_DATA,   POINTER_DATA};union EntryValue{   float floatValue;   double doubleValue;   short shortValue;   long longValue;   void* pointerValue;};class Entry{public:   Entry(float f);   Entry(double d);   Entry(short s);   // ... etc ...   Entry& operator=(float f);   Entry& operator=(double d);   Entry& operator=(short s);   // ... etc ...   bool operator==(float f);   bool operator==(double d);   bool operator==(short s);   // ... etc ...   EntryType getType();   EntryValue getValue();private:   EntryType m_currType;   EntryValue m_currValue;};  


Then you just can create an array of Entry types. There''s probably an easier way but I can''t think of it right now.
http://www.boost.org/libs/any/index.html
Unfortunately the overloading idea creates a bit of a headache, but that link has given me some ideas, thanks.
There is also a dynamic_any experiment by the same people.

http://prdownloads.sourceforge.net/cpp-experiment/

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
why do you need it?

how will you use it?

is there a point in storing a whole load of unrelated things? (i know there can be times. Is it necessary for you, in what you are doing?)
its not %100 necessary, I've come across a use for it a couple of times when coding my AI, but I've got around needing it by using an array of pointers. The problem with pointers is you cant exactly save the pointer information directly to a file, so saving the AI's memory associations is going to be long and tedious. I also asked out of interest to see if it could be done

Thanks for the link Fruny.

[edited by - djones on January 31, 2003 5:57:34 AM]

This topic is closed to new replies.

Advertisement