How to give a name for an object

Discussion in 'C++' started by Jayden Shui, Nov 14, 2011.

  1. Jayden Shui

    Jayden Shui Guest

    Hello All,

    I have a system with a number of classes developed well. Now I want to
    add a name attribute to classes for input data. It gives each object a
    name for identification, which provide users convenience in input. The
    names will not affect the operation of the system. I don't want change
    existing code a lot. I am thinking using template such as

    template<class T>
    class Named : public T
    {
    public;
    // Get or Set name method.

    private:
    string mName;
    };

    But if class B is derived from A, Named<B> is not derived from
    Named<A>. This may give some problems.

    Any good way to do it? I appreciate your kind help.

    Best regards,

    Jayden
     
    Jayden Shui, Nov 14, 2011
    #1
    1. Advertising

  2. Jayden Shui

    Larry Evans Guest

    On 11/14/11 09:24, Jayden Shui wrote:
    > Hello All,
    >
    > I have a system with a number of classes developed well. Now I want to
    > add a name attribute to classes for input data. It gives each object a
    > name for identification, which provide users convenience in input. The
    > names will not affect the operation of the system. I don't want change
    > existing code a lot. I am thinking using template such as
    >
    > template<class T>
    > class Named : public T
    > {
    > public;
    > // Get or Set name method.
    >
    > private:
    > string mName;
    > };
    >
    > But if class B is derived from A, Named<B> is not derived from
    > Named<A>. This may give some problems.
    >
    > Any good way to do it? I appreciate your kind help.
    >
    > Best regards,
    >
    > Jayden

    I've not use boost serialization,
    but I remember briefly reading the docs or code and it seems
    they "register" a class and I think the user has to provide
    some sort of input/output routines. You might try something
    similar where your registration would simply provide a map
    from class name to input/output method. Then, when you read
    the class name, you dispatch to the input routine retrieved
    from the map to read the input into the named class.

    Maybe the boost users list would provide a more detailed and
    accurate description of how it's done, and you might get
    some ideas from that.

    -regards,
    Larry
     
    Larry Evans, Nov 14, 2011
    #2
    1. Advertising

  3. Jayden Shui

    Jayden Shui Guest

    On Nov 14, 11:03 am, Larry Evans <> wrote:
    > On 11/14/11 09:24, Jayden Shui wrote:
    >
    >
    >
    >
    >
    >
    >
    > > Hello All,

    >
    > > I have a system with a number of classes developed well. Now I want to
    > > add a name attribute to classes for input data. It gives each object a
    > > name for identification, which provide users convenience in input. The
    > > names will not affect the operation of the system. I don't want change
    > > existing code a lot. I am thinking using template such as

    >
    > > template<class T>
    > > class Named : public T
    > > {
    > > public;
    > >    // Get or Set name method.

    >
    > > private:
    > >    string mName;
    > > };

    >
    > > But if class B is derived from A, Named<B> is not derived from
    > > Named<A>. This may give some problems.

    >
    > > Any good way to do it? I appreciate your kind help.

    >
    > > Best regards,

    >
    > > Jayden

    >
    > I've not use boost serialization,
    > but I remember briefly reading the docs or code and it seems
    > they "register" a class and I think the user has to provide
    > some sort of input/output routines.  You might try something
    > similar where your registration would simply provide a map
    > from class name to input/output method.  Then, when you read
    > the class name, you dispatch to the input routine retrieved
    > from the map to read the input into the named class.
    >
    > Maybe the boost users list would provide a more detailed and
    > accurate description of how it's done, and you might get
    > some ideas from that.
    >
    > -regards,
    > Larry


    Wow! I learned a lot from you!!!

    Best regards,

    Jayden
     
    Jayden Shui, Nov 14, 2011
    #3
    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. Zachary Hilbun
    Replies:
    4
    Views:
    562
    Zachary Hilbun
    Dec 20, 2004
  2. corejavagroups
    Replies:
    8
    Views:
    424
    Luc The Perverse
    Nov 5, 2005
  3. ding feng
    Replies:
    2
    Views:
    2,848
    ding feng
    Jun 25, 2003
  4. Graeme Matthew
    Replies:
    4
    Views:
    329
    Michael Hudson
    Jul 15, 2003
  5. grocery_stocker
    Replies:
    10
    Views:
    640
    Keith Thompson
    May 25, 2005
Loading...

Share This Page