Jump to content

Recommended Posts

Posted

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!!!

trindieprod.png?dl=0spacer.png?dl=0steam-icon.png?dl=0twitter-icon.png?dl=0spacer.png?dl=0
Posted

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.

Posted

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.

  • Upvote 1
Posted

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;
}

Let's build cool stuff and have fun. :)

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...