Make object by it's name

Posted by Ockonal on Stack Overflow See other posts from Stack Overflow or by Ockonal
Published on 2011-01-16T11:41:44Z Indexed on 2011/01/16 11:53 UTC
Read the original article Hit count: 151

Filed under:
|
|
|

Hello, is it possible to return exemplar of object using passed type name (string) in c++? I have some base abstract class Base and a few derivates. Example code:

class Base
{
   /* ... */
};

class Der1 : public Base
{
   /* ... */
};

class Der2 : public Base
{
   /* ... */
};

And I need function like:

Base *objectByType(const std::string &name);

Number of derivates classes are changeable and I don't want to make something like switching of name and returning by hands new object type. Is it possible in c++ to do that automatically anyway?

p.s. usage should looks like:

dynamic_cast<Der1>(objectByType("Der1"));

I need pure c++ code (crossplatform). Using boost is permissible.

© Stack Overflow or respective owner

Related posts about c++

Related posts about class