Jump to content

Constructing objects from a file


tjheldna
 Share

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

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

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.

 Share

×
×
  • Create New...