0

I read from a file, and based on each line I create an object of a class with the same name as the first word of the line, this would require me to make "if statement" to check each word and then create the object. Is there any way to convert the string to class name, so I can do something like that:

string className = "someClass";
className obj;
Ahmed Waheed
  • 1,281
  • 5
  • 21
  • 39
  • And no, you can't do this. Languages with reflection can do this (e. g. Objective-C in the C family of languages). –  May 21 '13 at 08:42
  • Trying to get a class name from a file and then create instances of that class is a sign of bad design. You should consider alternative, cleaner (and clearer) ways of doing things before you jump into manually doing this. What problem are you really trying to solve? – Daniel Daranas May 21 '13 at 08:49

1 Answers1

1

No, you can't...
Declaration/definitions need to be known at compile-time. You can achieve runtime-polymorphism by building a class-hierarchy (and/or Interfaces), though.

bash.d
  • 13,029
  • 3
  • 29
  • 42