# C++: Instantiating classes by name

This topic is 4992 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In Java, you can instatiate a class at run-time by name. Obviously, this isn't possible in C++. But is there an elegant way to get similar results without having to do a long chain of strcmp's, with one check for every class I know I have?

##### Share on other sites
Lookup "abstract factory."

##### Share on other sites
strcmp()s ? You were talking of C++, werent you ? :D

In general, no, there's not much else you can do, apart from using an std::map or std::hash_map in order to optimize search time, eg.

struct Factory {  struct Creatable {    virtual ~Creatable() {}  };  virtual ~Factory() {}  virtual Creatable *createInstance() = 0;};template<typename ProductType>struct FactoryImpl {  // Replace by Factory::Creatable if compile doesn't support  // covariant return types  ProductType *createInstance() { return new ProductType(); }};int main() {  std::map<std::string, Factory *> Factories;  Factories["Tank"] = new FactoryImpl<Tank>();  Factories["Ship"] = new FactoryImpl<Ship>();  Factories["Plane"] = new FactoryImpl<Plane>();  Factory::Creatable *p = Factories["Ship"]->second->createInstance();}

-Markus-

##### Share on other sites
Thanks for the replies, guys. Unfortunately, where I work the powers that be are ideologically opposed to templates. So, basically, I foresee a ton of strcmps in my future.

##### Share on other sites
Quote:
 Original post by smitty1276Unfortunately, where I work the powers that be are ideologically opposed to templates.

*cries*

##### Share on other sites
Quote:
 Original post by smitty1276Unfortunately, where I work the powers that be are ideologically opposed to templates.

I'm with Polymorphic OOP on this one. Quit.

seriously.

##### Share on other sites
Quote:
Original post by ChaosEngine
Quote:
 Original post by smitty1276Unfortunately, where I work the powers that be are ideologically opposed to templates.

I'm with Polymorphic OOP on this one. Quit.

seriously.

That is strange that must mean your company is also opposed to using the complete C++ standard library (excluding the C part) to...

"did i just see them use std::cout?" "ah sorry according to company rules you can't use it as it's an instance of class template...."

That also means CLR or java or any language with generics are out of the question too.... okay i know its not exactly the same but still.

##### Share on other sites
Just code an abstract factory using templates and show them how much time and money it'll save. The moment that "time", "money" and "save" are uttered is usually the moment where managers begin to embrace templates.

Or like the people above me said, just leave and find a competent employer.

##### Share on other sites
Quote:
 Unfortunately, where I work the powers that be are ideologically opposed to templates

When hammering a nail in, do they prefer to hit it with the wooden end of a hammer?

##### Share on other sites
Quote:
 Original post by smitty1276Thanks for the replies, guys. Unfortunately, where I work the powers that be are ideologically opposed to templates. So, basically, I foresee a ton of strcmps in my future.

Why would anyone be 'ideologically' opposed to templates? I can't see how anyone who programs in C++ could be opposed to templates, they are one of the most powerful tools for generic programming!

1. 1
Rutin
26
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633312
• Total Posts
3011309
• ### Who's Online (See full list)

There are no registered users currently online

×