Archived

This topic is now archived and is closed to further replies.

Arrays of multiple data types

This topic is 5432 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
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?)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites