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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Template question in C++

Template question in C++

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++dockerdata-structureslearning
6 Posts 4 Posters 0 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.
  • G Offline
    G Offline
    grscot
    wrote on last edited by
    #1

    Dear all, I need to use an array of Book pointers (BookList) and Member (MemberList). Both of them are compound (container) classes. The component objects are books and members (up to 10)for each list respectively. Is the below implementation of the array.h file correct? #ifndef _ARRAY #define _ARRAY const int MAX_VALUE=10; template class Array { public: Array(int maximum=10); ~Array(); ...... private: Object* array_list[MAX_VALUE]; //To keep track of the elements into the list. int num_of_elements; }; #endif Regards, grscot

    A 1 Reply Last reply
    0
    • G grscot

      Dear all, I need to use an array of Book pointers (BookList) and Member (MemberList). Both of them are compound (container) classes. The component objects are books and members (up to 10)for each list respectively. Is the below implementation of the array.h file correct? #ifndef _ARRAY #define _ARRAY const int MAX_VALUE=10; template class Array { public: Array(int maximum=10); ~Array(); ...... private: Object* array_list[MAX_VALUE]; //To keep track of the elements into the list. int num_of_elements; }; #endif Regards, grscot

      A Offline
      A Offline
      Alvaro Mendez
      wrote on last edited by
      #2

      It looks OK, but does it compile? :-) If it compiles, the rest is a matter of making sure it has no bugs, which the debugger will help you determine. Now, as far as what I would do if I were you, I'd try to use a container class from one of the available libraries, be it MFC or STL. STL is more sophisticated and portable, but also more difficult for beginners. MFC is simpler and better documented. So take your pick: MFC: typedef **CTypedPtrArray**<CPtrArray, Book> BookList; STL: typedef **vector**< auto_ptr<Book> > BookList; Regards, Alvaro


      When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com

      A J 2 Replies Last reply
      0
      • A Alvaro Mendez

        It looks OK, but does it compile? :-) If it compiles, the rest is a matter of making sure it has no bugs, which the debugger will help you determine. Now, as far as what I would do if I were you, I'd try to use a container class from one of the available libraries, be it MFC or STL. STL is more sophisticated and portable, but also more difficult for beginners. MFC is simpler and better documented. So take your pick: MFC: typedef **CTypedPtrArray**<CPtrArray, Book> BookList; STL: typedef **vector**< auto_ptr<Book> > BookList; Regards, Alvaro


        When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com

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

        Thanks a lot Alvaro Mendez Regards, grscot

        1 Reply Last reply
        0
        • A Alvaro Mendez

          It looks OK, but does it compile? :-) If it compiles, the rest is a matter of making sure it has no bugs, which the debugger will help you determine. Now, as far as what I would do if I were you, I'd try to use a container class from one of the available libraries, be it MFC or STL. STL is more sophisticated and portable, but also more difficult for beginners. MFC is simpler and better documented. So take your pick: MFC: typedef **CTypedPtrArray**<CPtrArray, Book> BookList; STL: typedef **vector**< auto_ptr<Book> > BookList; Regards, Alvaro


          When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          std::vectors of auto_ptrs are a no-no. There are tons of articles explaining why this is not safe --briefly put, STL assumes objects inside a container have standard copy semantics, which auto_ptrs do not have. Instead, one can use boost::shared_ptr (see www.boost.org[^]). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          A 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            std::vectors of auto_ptrs are a no-no. There are tons of articles explaining why this is not safe --briefly put, STL assumes objects inside a container have standard copy semantics, which auto_ptrs do not have. Instead, one can use boost::shared_ptr (see www.boost.org[^]). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            A Offline
            A Offline
            Alvaro Mendez
            wrote on last edited by
            #5

            Wow, I had no idea. Could you point me to one of those articles you mention? I mean, I'd like to see under what circumstances a vector of auto_ptrs does not work as expected, so that I can steer clear of such scenarios. The auto_ptr class has a simple and straightforward copy constructor/assignment operator; it's hard to imagine that it wouldn't work as expected under typical usage. Thanks, Alvaro


            When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com

            J 1 Reply Last reply
            0
            • A Alvaro Mendez

              Wow, I had no idea. Could you point me to one of those articles you mention? I mean, I'd like to see under what circumstances a vector of auto_ptrs does not work as expected, so that I can steer clear of such scenarios. The auto_ptr class has a simple and straightforward copy constructor/assignment operator; it's hard to imagine that it wouldn't work as expected under typical usage. Thanks, Alvaro


              When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com

              J Offline
              J Offline
              Joaquin M Lopez Munoz
              wrote on last edited by
              #6

              See for instance, Danny Kalev's The Top 20 C++ Tips of All Time[^], tip 20. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

              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