Jump to content
  • Advertisement
Sign in to follow this  
smitty1276

C++: Instantiating classes by name

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


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

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

seriously.

Share this post


Link to post
Share on other sites
Quote:
Original post by ChaosEngine
Quote:
Original post by smitty1276
Unfortunately, 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by smitty1276
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.


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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!