我正在使用工厂模式.它基本上允许类在编译时注册并存储在映射中.然后可以使用BaseFactory :: createInstance()返回一个实例
我不确定地图在编译时如何持有类名!如何在运行时有效的编译时分配内存?
在这种情况下,所有类都是从父类Bump_BaseObject派生的
//C++ STL used for adding Reflection
#include <string>
#include <map>
class Bump_BaseObject;
/**
* Derived Base objects creation factory
*/
template<typename T>
Bump_BaseObject* createT(void)
{
#pragma message("createT instantiated")
return new T();
}
struct BaseFactory {
typedef std::map<std::string, Bump_BaseObject*(*)()> map_type;
//return an instance of the class type 's'
static Bump_BaseObject* createInstance(const std::string& s) {
map_type::iterator it = getMap()->find(s);
if(it == getMap()->end())
return 0;
//this is where we instatiate and allocate memory for the object(it must NOT …Run Code Online (Sandbox Code Playgroud)