Is it possible to create a Factory that generates all derived classes (and class names) at runtime, based on all the classes that are specified in a particular file?
For example, given a specific file where users are constantly adding derived classes:
class Laptop: public Computer {
public:
virtual void Run(){mHibernating = false;}
virtual void Stop(){mHibernating = true;}
private:
bool mHibernating; // Whether or not the machine is hibernating
};
class Desktop: public Computer {
public:
virtual void Run(){mOn = true;}
virtual void Stop(){mOn = false;}
private:
bool mOn; // Whether or not the machine has been turned on
};
// ....[more classes derived from Computer]...
Can a Factory generate a list of possible classes to map to, based on their name?
class ComputerFactory {
public:
static Computer *NewComputer(const std::string &description)
{
if(description == "Laptop")
return new Laptop;
} else if (description == "Desktop") {
return new Desktop;
} else if // ...[more comparisons based on the classes defined at runtime]...
} else return NULL;
}
};