constructing objects from a stream

J

jesper

Hi.
This might be to basic, or of topic, or just plain silly. But, Is there
a nice way to
construct objects dynamically from a data stream (file, socket, memory)
without
prior enumerating classes?
With enumerating classes I mean solutions like:
(pseudo code 1)
class B
{
tClasses type;
B(tClasses iType=classB){}
};

class D:public B
{
D():B(classD){}
};

B* CreateObject(tClasses Class)
{
switch(Class)
{
case classD: return new D;
}
}
(end pseudo code 1)
or
(pseudo code 2)
map<tClasses,CreateObjectFunctionPtr> CreateFunctions;
....
D(){CreateFunctions[tClasses]=CreateClassDObject;}
....
(end pseudo code 2)

My question is, is there a way in the standard that involves less work?
(I might be
as lazy as they come), I have a project with a map of objects which can
be any of 200 or so classes. Now I need to save this map to file and
restore it. Any Input?
 
A

Alf P. Steinbach

* (e-mail address removed):
Hi.
This might be to basic, or of topic, or just plain silly. But, Is there
a nice way to
construct objects dynamically from a data stream (file, socket, memory)
without
prior enumerating classes?
With enumerating classes I mean solutions like:
(pseudo code 1)
class B
{
tClasses type;
B(tClasses iType=classB){}
};

class D:public B
{
D():B(classD){}
};

B* CreateObject(tClasses Class)
{
switch(Class)
{
case classD: return new D;
}
}
(end pseudo code 1)
or
(pseudo code 2)
map<tClasses,CreateObjectFunctionPtr> CreateFunctions;
...
D(){CreateFunctions[tClasses]=CreateClassDObject;}
...
(end pseudo code 2)

My question is, is there a way in the standard that involves less work?
(I might be
as lazy as they come), I have a project with a map of objects which can
be any of 200 or so classes. Now I need to save this map to file and
restore it. Any Input?

To avoid work you should probably use some library solution such as e.g.
boost's serialization support (haven't used it myself). But in order to
do that you need to understand serialization, which is what you don't.
First, it's explained in the FAQ (but that might be hard to understand),
second, I've written a little about it* at the absolute novice level
(but I use a lot of words to get to the point), and third, I'm sure
you'll find it explained in some textbook -- but which one?

Hth.,

- Alf


*) <url: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top