Creating array of data types

M

Madhur

Hi All,

I would like you help me in creating an array of data types.

I am interested in look at the the data type which looks like this

Array a[10]={int,float,char,int*..............................},

so that a[0] should return me int and a[1] should return me
float..which helps me in runtime type casting.

I do not know how to create such an array. If i create so what would
be the data type of "Array".
This is basically a variable to "data type" conversion.

Or is there any better way to do this.

Looking keenly for the response..

Regards,
Madhur
 
A

Alf P. Steinbach

* Madhur:
Hi All,

I would like you help me in creating an array of data types.

I am interested in look at the the data type which looks like this

Array a[10]={int,float,char,int*..............................},

so that a[0] should return me int and a[1] should return me
float..which helps me in runtime type casting.

C++ doesn't have such arrays. And type casting is Evil. So best advice
is: try to tell us about the real problem, not your imagined solution.

I do not know how to create such an array. If i create so what would
be the data type of "Array".
This is basically a variable to "data type" conversion.

Or is there any better way to do this.

Yes, certainly, but you haven't told what "this" -- the real problem
-- is. The imagined solution with silly-array is not the real problem.

Looking keenly for the response..

Try to post some code (preferably that compiles).
 
J

Junhui Tong

Madhur said:
Hi All,

I would like you help me in creating an array of data types.

I am interested in look at the the data type which looks like this

Array a[10]={int,float,char,int*..............................},

There is a "variant" approach:

struct variant {
enum {vt_int, vt_float, vt_char, vt_intp, ...} type;
union {
int v_i;
float v_f;
char v_c;
int * v_ip;
// ...
};
};

then you can write code like this:
variant a[10];
a[0].type = vt_int; a[0].v_i = 4;
a[1].type = vt_char; a[1].v_c = 'f';
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top