Storing objects in map class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeet232
    New Member
    • Jul 2007
    • 22

    #1

    Storing objects in map class

    Hi,
    I've these classes

    Integer derived from Object
    String derived from Object

    I'm creating another class MyMap
    which shall have the capability to store Integer, String (or any other obj derived from 'Object' class) objects in it. MyMap has a data member of stl map.

    Can u plz give me a tiny program showing how to store Integer/String objs in MyMap. MyMap is not inherited from anything, it just use stl map as data member. providing an interface to other classes for manipulating stl map easily.

    I'm getting into lot of trouble doing this, please help.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by jeet232
    Hi,
    I've these classes

    Integer derived from Object
    String derived from Object

    I'm creating another class MyMap
    which shall have the capability to store Integer, String (or any other obj derived from 'Object' class) objects in it. MyMap has a data member of stl map.

    Can u plz give me a tiny program showing how to store Integer/String objs in MyMap. MyMap is not inherited from anything, it just use stl map as data member. providing an interface to other classes for manipulating stl map easily.

    I'm getting into lot of trouble doing this, please help.
    Check this
    [code=cpp]
    map<int,string> int_string_map;
    string s1("abc");
    int_string_map[1]=s1;
    int_string_map[2]=s1;


    [/code]

    Hopw this is what u need

    Thanks
    Raghuram

    Comment

    • jeet232
      New Member
      • Jul 2007
      • 22

      #3
      Originally posted by gpraghuram
      Check this
      [code=cpp]
      map<int,string> int_string_map;
      string s1("abc");
      int_string_map[1]=s1;
      int_string_map[2]=s1;


      [/code]

      Hopw this is what u need

      Thanks
      Raghuram
      Hi Raghuram,
      thanks for reply, but can u pls tell me how the 'get' functionality will be achieved. like say:

      [code=cpp]
      map<string,stri ng> str_string_map;
      string s1("abc");
      [/code]

      Then in this case, how the insertion & retreival will work?

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by jeet232
        Hi Raghuram,
        thanks for reply, but can u pls tell me how the 'get' functionality will be achieved. like say:

        [code=cpp]
        map<string,stri ng> str_string_map;
        string s1("abc");
        [/code]

        Then in this case, how the insertion & retreival will work?
        HI,
        Read this Map tutorial to get a good idea about the map.
        Thanks
        Raghuram

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          [quote=jeet232]
          I've these classes

          Integer derived from Object
          String derived from Object
          [/code]

          You have your solution here. MyMap must contain Objects. Plus a key. More to the point, a handle to an Object plus a key:
          [code=cpp]
          map<int, Handle<Object> > MyMap;
          [/code]

          1) Read the article on Handle classes.
          2) Read the article on Visitor. You will need a Visitor to access specific methods in your derived classes that are not in tyour base class.
          3) Your entire application must use Handle<Object>.
          4) Get a design patterns book. Look up Template Method. You will need this too.

          Comment

          Working...