Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > constructing objects from a stream

Reply
Thread Tools

constructing objects from a stream

 
 
jesper@alphacash.se
Guest
Posts: n/a
 
      02-23-2006
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 Dublic 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?

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-23-2006
* http://www.velocityreviews.com/forums/(E-Mail 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 Dublic 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>

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
constructing vector<POD> that is member of a class from an input stream (file) Hicham Mouline C++ 3 11-18-2009 04:17 PM
Constructing a URI that uses a stream as input Qu0ll Java 0 07-04-2009 03:40 PM
Constructing MIME message without loading message stream Kris Kennaway Python 2 08-10-2008 11:21 PM
concurrency constructing objects Archimede Java 5 12-01-2005 11:42 AM
Ways of constructing objects. Robbie Hatley C++ 2 07-04-2004 08:43 AM



Advertisments