0

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";

Edited by tfmontague: n/a

5
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by alwaysLearning0
1

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;
   };
};

Edited by Ancient Dragon: n/a

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.