有办法从保存类名的字符串中实例化对象吗?
class Base;class DerivedA : public Base;class DerivedB : public Base;/*etc...*/
#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...*/) { /*etc...*/ } };private: string msClassName;};/*etc.*/
PIPIONE
尚方宝剑之说
相关分类