Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++ how to convert a string to class object?

For example i have a class named "WaterScene", in the .xml i have saved as string "WaterScene" when i read the .xml i have to convert that string to class. One approach is just string comparison

if( string == "WaterScene")
return new WaterScene;

Is there any generic way to do this to avoid string comparison? Like in objective C(dynamic language) we can get the class using string...

Class classObject =[[NSBundle mainBundle] classNamed:string];
like image 745
Chandan Shetty SP Avatar asked Jul 14 '11 12:07

Chandan Shetty SP


2 Answers

If all of the objects you would be returning are derived from a common base class, you could use a std::map<std::string,BaseClass *>. The comparisons ultimately in there somewhere, but it keeps things better organized.

like image 106
jonsca Avatar answered Nov 15 '22 10:11

jonsca


No, you can't do it with standard C++. C++ has no notion of reflection. Sorry :)

like image 33
Armen Tsirunyan Avatar answered Nov 15 '22 08:11

Armen Tsirunyan