tjheldna Posted November 7, 2013 Share Posted November 7, 2013 Hi All, I'm trying to make my code more efficient and have come across a problem that I'm not sure how I can get around and I’m banging my head against the wall. enum { typeSpawn = 'spaw' }; //Base class class Object { long type; public: Object(long type) }; //Derived class class Spawn : public Object { public: Spawn(long type) }; Say each object that needs to be saved has a 'type' for this example the inherited class is a spawn. When Loading I need to re-create these objects with the type being the only identifier for what to type of object to create. At the moment I have a switch statement long type = typeReadFromFile Object *object = Null; Switch(type) { case typeSpawn: { object = new Spawn(); break; } } This works however it would be nice to be able to call a base class function object->Create(type) which acts like a virtual constructor (which is not possible in C++). This will save having to add a new case each time I create a new object which needs serialisation. Anyone have any info or ideas on how to solve this problem? Cheers!!! Quote Link to comment Share on other sites More sharing options...
Rick Posted November 7, 2013 Share Posted November 7, 2013 Factory pattern? but in any case (as far as I know) in C++ you'll have to add some kind of code for a new class since it lacks reflection itself. Either a switch like you have, or registering a class object by creating it for the factory pattern. Quote Link to comment Share on other sites More sharing options...
tjheldna Posted November 7, 2013 Author Share Posted November 7, 2013 I'll research Factory Pattern, I just don't know what to search for! Cheers, Rick Quote Link to comment Share on other sites More sharing options...
Rick Posted November 7, 2013 Share Posted November 7, 2013 http://www.codeproject.com/Articles/363338/Factory-Pattern-in-Cplusplus Notice this section in the factory: AnimalFactory::AnimalFactory() { Register(“Horse”, &Horse::Create); Register(“Cat”, &Cat::Create); Register(“Dog”, &Dog::Create); Register(“Spider”, &Spider::Create); } With a stl::map you can link a string to a static class function called something like Create() that returns a pointer to that class type (note the return type of the Create function needs to be a pointer to a base class). Just note instead of a switch to decide you are moving it to a stl::map and having to call a Register() method for each possible type. I think this is cleaner than a switch, but more complicated as you're storing a function pointer in a map. 1 Quote Link to comment Share on other sites More sharing options...
Josh Posted November 9, 2013 Share Posted November 9, 2013 Use a static member that is initialized to call the factory constructor: class Factory { static std::map<std::string,Factory*> map; }; class SpawnFactory : public Factory { static SpawnFactory* factory; }; SpawnFactory::factory = new SpawnFactory SpawnFactory::SpawnFactory() { this->name = "Spawn" map[name]=this; } Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.