constructing objects from a stream

Discussion in 'C++' started by jesper@alphacash.se, Feb 23, 2006.

  1. Guest

    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?
    , Feb 23, 2006
    #1
    1. Advertising

  2. * :
    > 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>

    --
    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?
    Alf P. Steinbach, Feb 23, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Archimede

    concurrency constructing objects

    Archimede, Nov 25, 2005, in forum: Java
    Replies:
    5
    Views:
    405
    Chris Uppal
    Dec 1, 2005
  2. Robbie Hatley

    Ways of constructing objects.

    Robbie Hatley, Jul 4, 2004, in forum: C++
    Replies:
    2
    Views:
    288
    Jonathan Turkanis
    Jul 4, 2004
  3. Kris Kennaway
    Replies:
    2
    Views:
    357
    Kris Kennaway
    Aug 11, 2008
  4. Qu0ll
    Replies:
    0
    Views:
    330
    Qu0ll
    Jul 4, 2009
  5. Hicham Mouline
    Replies:
    3
    Views:
    466
    red floyd
    Nov 18, 2009
Loading...

Share This Page