Declaration variable of type specified by string value

Hello. I wonder if there is any way to declare variable of type specified by string value. For example, I get "float" and need to declare float-variable. Of course, I can do

1
2
3
4
if (str == "float")
{
     float f = 0;
}


for each variable type, but maybe exists better solution. Or at least get sizeof(float). Thanks.
You are better off designing a full polymorphic class hierarchy for this. I am assuming of course that you want the variables to last longer than the scope of the if statement and also that you plan to allow the user to then modify these variables - sounds very much like a strongly-typed scripting language of your own design ;)
The basic problem is you're treating compile time and run time ideas.

You need code to deal with it. Have you looked at unions?
http://en.wikipedia.org/wiki/Union_type#C.2FC.2B.2B
http://en.cppreference.com/w/cpp/language/union
Topic archived. No new replies allowed.