9
有没有办法从持有类名的字符串中实例化对象?
我有一个文件:Base.h class Base; class DerivedA : public Base; class DerivedB : public Base; /*etc...*/ 和另一个文件:BaseFactory.h #include "Base.h" class BaseFactory { public: BaseFactory(const string &sClassName){msClassName = sClassName;}; Base * Create() { if(msClassName == "DerivedA") { return new DerivedA(); } else if(msClassName == "DerivedB") { return new DerivedB(); } else if(/*etc...*/) { …