How do I make an array (or an abstract data type) which can hold different data types? Then use those data types to create instances.

I want to do the following:

int arrayType[3] = {bool, int, char};

 /*arrayType[0]*/  myBool = true;
 /*arrayType[1]*/  myInt = 2;
 /*arrayType[2]*/  myChar = "a";

Recommended Answers

All 6 Replies

create a union

// declare types
enum data_types {
    undefined_type = 0,
    bool_type,
    char_type,
    short_type,
    ushort_type,
    int_type,
    uint_type,
    // etc for each type you want to support
};

    

struct data
{
    data_types valid_type;
    union types
    {
         bool b;
         char c;
         short s;
         unsigned short us;
         int i;
         unsigned int ui;
         long l;
         unsigned long ul;
         long long ll;
        float f;
        double d;
   };
};

ummm, not sure why but how about this idea:

struct EmptyType{}

template<typename T1,typename T2 = EmptyType(),...and so on for some finite amount>
struct TupleType{
 typedef type1 T1;
 typedef type2 T2;
 //...and so on
}

int main(){
 Tuple<int,bool,char,string>
 Tuple::type1 intType = 0;
 Tuple::type2 boolType = false;
 //...
}

What exactly are you trying to do?

this is a year old :)

Oh damn, wondering how come this thread popped up in the list 1 of the page?

someone bumped it but his posts was deleted

btw, i like the Typelist idea, its a great idea to do lot of things. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.