-
Variable-type variable?
Hi,
I am in need of a variable that can contain any variable type. Something like this:
Code:
VARTYPE Var;
Var = int;
Var = double;
I know I can simply declare a bunch of constant numbers, who each represents a variable type. But that would be very non-optimal if there already exists an easier way. Could I use templates to achieve this or the use of preprocessors/macros?
Any help or tips are highly appreciated. :)
-
A union would perhaps be the best solution here:
Code:
typedef union
{
int iVal;
short int sVal;
long int lVal;
char cVal;
float fVal;
double dVal;
}MultiVal;
int main()
{
MultiVal MyVal = {0};
MyVal.iVal = 65;
cout << MyVal.cVal << endl; //Prints the ASCII character for 65, which is 'A'
return 0;
}
-
-
hmm... ok, sorry I might have been a bit unspecific.
What I want is a variable that only holds a variable-type (like: int or double) and not its value. For ex:
Code:
VARTYPE Type;
Type = int; // the type is integer.
switch (Type) {
case short: // The type is short
//...
break;
case int: // The type is integer
//...
break;
case float: // The type is float
//...
break;
case double: // The type is double
//...
break;
}
-
I don't think you would be able to use the actual type names, those are kind of reserved. I'd do what Magos said, seems like the best way.
-
Maybe you could use a void pointer??
Code:
void *Type;
Type = new int(0);
switch(sizeof(*Type)){
case 1: //char
case 2: //short
etc..
Just a suggestion, i've never tried it...
-
That's a nice idea, but aren't some variable types the same size as each other? Like, signed/unsigned etc.
-
Yes, they are, but you could make a crude check..
-
I think some context would help here. I find it hard to imagine the purpose of the code, so I can't really think of any solutions.
-
Missing javascript, are we? :D
Here's one way:
Code:
struct variable
{
enum var_type
{
unassigned,
string_type,
double_type
};
var_type type;
string string_rep;
double double_rep;
variable & operator += (const char * str)
{
if(type == unassigned)
type = string_type;
if(type == string_type)
{
string_rep += str;
}
else if(type == double_type)
{
double_rep += strtod(str, 0);
}
return *this;
}
variable & operator += (double val)
{
if(type == unassigned)
type = double_type;
if(type == string_type)
{
char str[16];
sprintf(str, "%f", val);
string_rep += str;
}
else if(type == double_type)
{
double_rep += val;
}
return *this;
}
variable & operator = (const char * str)
{
if(type == unassigned)
type = string_type;
if(type == string_type)
{
string_rep = str;
}
else if(type == double_type)
{
double_rep = strtod(str, 0);
}
return *this;
}
variable & operator = (double val)
{
if(type == unassigned)
type = double_type;
if(type == string_type)
{
char str[16];
sprintf(str, "%f", val);
string_rep = str;
}
else if(type == double_type)
{
double_rep = val;
}
return *this;
}
variable & operator += (variable & val)
{
if(val.type == string_type)
{
return *this += val.string_rep.c_str();
}
else if(val.type == double_type)
{
return *this += val.double_rep;
}
return *this;
}
variable & operator = (variable & val)
{
if(val.type == string_type)
{
return *this = val.string_rep.c_str();
}
else if(val.type == double_type)
{
return *this = val.double_rep;
}
return *this;
}
variable(const char * str)
:type(string_type)
{
*this = str;
}
variable(double val)
:type(double_type)
{
*this = val;
}
variable()
:type(unassigned), double_rep(0), string_rep("")
{
}
friend ostream & operator << (ostream & out, variable & _this)
{
if(_this.type == variable::string_type)
{
out << _this.string_rep;
}
else if(_this.type == variable::double_type)
{
out << _this.double_rep;
}
return out;
}
friend istream & operator >> (istream & in, variable & _this)
{
if(_this.type == variable::string_type)
{
in >> _this.string_rep;
}
else if(_this.type == variable::double_type)
{
in >> _this.double_rep;
}
return in;
}
};
Code:
int main()
{
variable str = "This is ";
variable num = 1;
str += num;
str += " way to do it!";
cout << str << endl;
str = "255";
num += str;
cout << num << endl;
cin.get();
}
-
So basically, the idea behind this is to overload the operators for each type we want to look at? That way, we can set the variable type, and subsequent operations can be dealt with accordingly, right?
-
Thanks for your replys but it seems like there ain't no better way than making a list of all the variable types and representing each of them with their own unique "Id" number. Life can be crule some times :(
-
So what kind of program was this code part of? I'm curious as to why you'd need to implement something like this.
-
C++, by design, is much more type-safe than many other languages. There are ways of getting around this (unions and void* come to mind), but in general, it is a bad idea to try to circumvent the type-safety. Indeed, if you tell us your application for this, we may be able to help you come up with a better design.
Cheers