An array with multiple data types

Started by
16 comments, last by LAURENT* 9 years, 8 months ago

My program was running smoothly until I needed my array to store into data types. After I ran the code the screen went blink and I could run my program again until I remove the into data type. Can sia force my array to store different data types? I really really need it to do this for my level renderer to be perfect.

Advertisement

Okay, you might actually need to tell us which language you are using here wink.png (and what is an "into data type"? typo?)

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

You can do different data type arrays in Python, Ruby, PHP and JavaScript

Are you looking for something like a HashMap or Dictionary ?

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

I code in c++ and I really meant int data type but I got auto corrected.

If it's for something low level like a renderer, you might want to rethink some things for performance sake, Having multiple datatypes in a single array will require a lot of checking which is probably something you do not want.

We're missing a lot of information on what you actually want to achieve, but considering you are talking about a renderer, I assume you want to render different level objects? Perhaps consider having a renderable (or anything) base class that has a render function, but more info would be nice! :)

Bacterius already mentioned that knowing what the language you're working with is something nice to know, also what kind of data types you are referring to and what they should be doing to have your level renderer work.

I code in c++ and I really meant int data type but I got auto corrected.

You can cheat and use an array of pointers - however this can get very tricky to maintain and not recommended .

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

If it's for something low level like a renderer, you might want to rethink some things for performance sake, Having multiple datatypes in a single array will require a lot of checking which is probably something you do not want.

We're missing a lot of information on what you actually want to achieve, but considering you are talking about a renderer, I assume you want to render different level objects? Perhaps consider having a renderable (or anything) base class that has a render function, but more info would be nice! smile.png

Bacterius already mentioned that knowing what the language you're working with is something nice to know, also what kind of data types you are referring to and what they should be doing to have your level renderer work.

I code in c++ programming language. I need my array to hold 2 data types. int data type and a special one I don't feel like explaining. I need the int data type to be stored so my renderer can place my object correctly base on reading from numbers from a special file. Overall I just really need to store different data types in a array. This will be the only array to do this in my program.

I think you could use an array containing two arrays each holding oen type.

If you mean each array element can either be an int or your other type, you can check out unions.

Basically youd have something like:

struct ArrayElement {

bool isThisAnObject; //use this to tell your code whether the type is MyObject or int so it can read/modify the correct one (assuming you cant deduce this some other way)

union { //object and integer overlap in memory/share some of the bytes, so you an only use one at a time or else face corruption of data

MyObject object;

int integer;

} data;

}

as the data type of the array. Note that all the elements of the array will be of size max(sizeof(MyObject),sizeof(int)) so you wont be saving any RAM by having some of the elements be int, AND you probably need to pay the extra byte to tell your code the actual type of each array element.

But it would be better if you give more information about the actual problem to see if theres some other more clean solution.

o3o

You might want to try boost::variant.

This topic is closed to new replies.

Advertisement