Hello all,
Is there a way to determine a variable's type at run-time? The reason
I'm asking is that i have code that looks like this:
template <class T>
Object::Object( int TypeCode, T* data)
{
switch (TypeCode)
case 1:
//Check to see if data is correct type, //according to a lookup table of my own
(1 is //int, 2 for char, etc.)
}
So, I will pass the function a pointer to a variable of a particular
type, and a numeric code representing what that type is. I want to make
sure the code and the data match, and if not, return an error(probably
just print some text, but that's not important). I know I can do this
with manually overloaded constructors, like:
Object::Object( int TypeCode, int* data)
{
if(TypeCode != 1)
{ //Error
}
}
Object::Object( int TypeCode, char* data)
{
if(TypeCode != 2)
{ //Error
}
}
But this can get big and complicated, and I'd rather not.
Is there some alternate way to do this with inheritance, maybe, or is
there a function that tells you, in some form, what type a variable is?
Comparing to make sure two variables are the same type would work, too.
Is there a way to determine a variable's type at run-time? The reason
I'm asking is that i have code that looks like this:
template <class T>
Object::Object( int TypeCode, T* data)
{
switch (TypeCode)
case 1:
//Check to see if data is correct type, //according to a lookup table of my own
(1 is //int, 2 for char, etc.)
}
So, I will pass the function a pointer to a variable of a particular
type, and a numeric code representing what that type is. I want to make
sure the code and the data match, and if not, return an error(probably
just print some text, but that's not important). I know I can do this
with manually overloaded constructors, like:
Object::Object( int TypeCode, int* data)
{
if(TypeCode != 1)
{ //Error
}
}
Object::Object( int TypeCode, char* data)
{
if(TypeCode != 2)
{ //Error
}
}
But this can get big and complicated, and I'd rather not.
Is there some alternate way to do this with inheritance, maybe, or is
there a function that tells you, in some form, what type a variable is?
Comparing to make sure two variables are the same type would work, too.
Comment