Jump to content
  • Advertisement

Archived

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

djones

Arrays of multiple data types

This topic is 5649 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
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.

Share this post


Link to post
Share on other sites
Unfortunately the overloading idea creates a bit of a headache, but that link has given me some ideas, thanks.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!