
Steffen Roeber wrote:
My preferred way would be something like a factory:
...
I think there is no "normal" C++ language support. But Boost has some methods that by-pass some language constraints. I took a loot at something like 'typeof'. But I don't get the things together.
The trunk version of Boost provides a ready-made factory as a function object. It can be assigned to a Boost.Function with an appropriate signature if you need an opaque, polymorphic handle. E.g: #include <boost/functional/factory.hpp> #include <boost/function.hpp> #include <map> typedef boost::function< an_abstract_class*() > a_factory; // [...] int main() { std::map<std::string,a_factory> factories; factories["a_name"] = boost::factory<a_concrete_class*>(); factories["another_name"] = boost::factory<another_concrete_class*>(); // [...] an_abstract_class* something = factories["a_name"](); // [...] } Regards, Tobias