Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. STL Container of classes

STL Container of classes

Scheduled Pinned Locked Moved ATL / WTL / STL
c++comdockertoolstutorial
6 Posts 4 Posters 44 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    James Pullicino
    wrote on last edited by
    #1

    Hi, I tried to make a std::map of classes like this: class A { ... }; typedef map<long, A> AMAP; As soon as I began experimenting I realized that things were not as simple as I hoped. For example, this code: AMAP amap; amap[10]; Will generate 1 call to A's default constructor and two calls to A's copy constructor. Before I continue experimenting to find the best way to make a map of classes I'd like to hear some advice from people who have already tried this sort of thing. Two specific questions I have are: 1) Is it advisable to have a map of classes? 2) Does anyone know the best way to acheive this? Any other advice/comments/talk about the subject will be appriciated. P.S. My class will not only consist of built-in types, but will also have pointers. Thanks, James Drinking In The Sun Forgot Password?

    A P 2 Replies Last reply
    0
    • J James Pullicino

      Hi, I tried to make a std::map of classes like this: class A { ... }; typedef map<long, A> AMAP; As soon as I began experimenting I realized that things were not as simple as I hoped. For example, this code: AMAP amap; amap[10]; Will generate 1 call to A's default constructor and two calls to A's copy constructor. Before I continue experimenting to find the best way to make a map of classes I'd like to hear some advice from people who have already tried this sort of thing. Two specific questions I have are: 1) Is it advisable to have a map of classes? 2) Does anyone know the best way to acheive this? Any other advice/comments/talk about the subject will be appriciated. P.S. My class will not only consist of built-in types, but will also have pointers. Thanks, James Drinking In The Sun Forgot Password?

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      One solution I found was to use pointers instead: typedef map AMAP; amap[10] = new A; This negates the cost of default and copy constructors. However, it does mean that you then have to manage the deletion of the pointers yourself, as empyting the map will just remove them from the map, not from memory. Unfortunatley, auto_ptr does not work here! I would be interested to see if there are any other solutions, as it seems to be very costly to use anything other than basic types in a container

      N 1 Reply Last reply
      0
      • A Anonymous

        One solution I found was to use pointers instead: typedef map AMAP; amap[10] = new A; This negates the cost of default and copy constructors. However, it does mean that you then have to manage the deletion of the pointers yourself, as empyting the map will just remove them from the map, not from memory. Unfortunatley, auto_ptr does not work here! I would be interested to see if there are any other solutions, as it seems to be very costly to use anything other than basic types in a container

        N Offline
        N Offline
        Neville Franks
        wrote on last edited by
        #3

        I agree that using new and storing pointers is the way to go for any of the STL containers if you don't want the default ctor's and dtor's called unneccessarily. I'd prefer to delete them myself, however the Boost shared_pointer should work fine here, not that I've tried such a thing. Re. your original question. I guess it depends why you need a collection (multiple instances) of the class in the first place. Neville Franks, Author of ED for Windows. www.getsoft.com

        J 1 Reply Last reply
        0
        • N Neville Franks

          I agree that using new and storing pointers is the way to go for any of the STL containers if you don't want the default ctor's and dtor's called unneccessarily. I'd prefer to delete them myself, however the Boost shared_pointer should work fine here, not that I've tried such a thing. Re. your original question. I guess it depends why you need a collection (multiple instances) of the class in the first place. Neville Franks, Author of ED for Windows. www.getsoft.com

          J Offline
          J Offline
          James Pullicino
          wrote on last edited by
          #4

          I have in fact resorted to pointers for now, which I agree is the best way to go overall. I would like to find out the best way to have a collection of classes without resorting to pointers, even if it worse than using pointers. I do not have any particular reason why I need a collection of the class - this is purley research. Thanks, James Drinking In The Sun Forgot Password?

          1 Reply Last reply
          0
          • J James Pullicino

            Hi, I tried to make a std::map of classes like this: class A { ... }; typedef map<long, A> AMAP; As soon as I began experimenting I realized that things were not as simple as I hoped. For example, this code: AMAP amap; amap[10]; Will generate 1 call to A's default constructor and two calls to A's copy constructor. Before I continue experimenting to find the best way to make a map of classes I'd like to hear some advice from people who have already tried this sort of thing. Two specific questions I have are: 1) Is it advisable to have a map of classes? 2) Does anyone know the best way to acheive this? Any other advice/comments/talk about the subject will be appriciated. P.S. My class will not only consist of built-in types, but will also have pointers. Thanks, James Drinking In The Sun Forgot Password?

            P Offline
            P Offline
            peterchen
            wrote on last edited by
            #5

            both the default CTor and the 2 copies come from the insertion of a default value, if the requested key is not found. So if you use:

            tmap::iterator it = map.find(key);
            
            if (it != map.end())
               // it->second is a ref to the value, you can use, or assign it
            else
               // key is not found - what do you want to do?
            

            Frankly, map is my single most used STL class, and I find myself always coding around the operator[]. The two copies come from 1) creating a temp pair that is passed to insert, and 2) copying the pair into the map (frankly, a good library design would not need one of the copies) You can ignore that if you have classes with "cheap" Copy-CTor's for default-constructed classes, and do complex construction after inserting a default, like this: value & v = map[10]; v.ReadDataFromHugeFileAndKeepIt(); the same is true for classes that hold System resources (like HANDLE's that are closed in the DTor) and have no acceptable copy contructor. Peter


            One day I might find it quite amusing how touching tongues make life so confusing  Anne Clark again   [sighist]

            J 1 Reply Last reply
            0
            • P peterchen

              both the default CTor and the 2 copies come from the insertion of a default value, if the requested key is not found. So if you use:

              tmap::iterator it = map.find(key);
              
              if (it != map.end())
                 // it->second is a ref to the value, you can use, or assign it
              else
                 // key is not found - what do you want to do?
              

              Frankly, map is my single most used STL class, and I find myself always coding around the operator[]. The two copies come from 1) creating a temp pair that is passed to insert, and 2) copying the pair into the map (frankly, a good library design would not need one of the copies) You can ignore that if you have classes with "cheap" Copy-CTor's for default-constructed classes, and do complex construction after inserting a default, like this: value & v = map[10]; v.ReadDataFromHugeFileAndKeepIt(); the same is true for classes that hold System resources (like HANDLE's that are closed in the DTor) and have no acceptable copy contructor. Peter


              One day I might find it quite amusing how touching tongues make life so confusing  Anne Clark again   [sighist]

              J Offline
              J Offline
              James Pullicino
              wrote on last edited by
              #6

              Good advice, thanks. James Drinking In The Sun Forgot Password?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups