Jump to content
  • Advertisement
Sign in to follow this  
Samurai Jack

Automated file loader (Not quite class factory)

This topic is 4028 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings! I have a C++ problem regarding a file loader. I would like to have somekind of automated loader. Let us say, we have a few classes:
class Person (which can load/save name, surname, age)
class Company (which can load/save factory details)
class Date (which can load/save date & time)
What I could do is some kind of enumerators and a class factory
char code = getchar(fs);
switch (code)
case 1: { Person* p = new Person(); p.Load(fs); };
case 2: { Company* c = new Company(); c.Load(fs); };
case 3: { Date* d = new Date(); d.Load(fs); };
I do not like this too much because allways when you add a new object like:
class Airplane() (which can laod/save passanger data, fuel etc..)
You allways have to update the class factory. It would be great if Airplane could inherit from a base class and as soon it is inherited it has all the capabilities to load and save. So when you define a class like Airplane it can in theory be loaded. Or do I have to do something like a header ".h" file, which contains all classes? Like:
BaseClass* array[] = { new Person, new Company, new Date };
and then allways when a code is read out of a file, I would iterate the array like this:
for each object in array do if array[current].CodeValid(code_from_file) then base* pObject = array[current].CreateNew();
Does someone have an idea or suggestion?

Share this post

Link to post
Share on other sites
You could use factory.
Store the class name in the file instead of a code and then, use factory to spawn the right class.

char *className = readStringFromFile (fs);
BaseClass *c = Factory::SpawnClass (className);
c->Load (fs);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!