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. C / C++ / MFC
  3. Collection Class Question

Collection Class Question

Scheduled Pinned Locked Moved C / C++ / MFC
11 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.
  • C Offline
    C Offline
    CDuddley
    wrote on last edited by
    #1

    Say I have an array Stuff[100] how would I make that into a collection class I'm having trouble understanding this. I need to derive it from the CObList in MFC. Thanks for your time guys. -CDudd:)

    C J T 3 Replies Last reply
    0
    • C CDuddley

      Say I have an array Stuff[100] how would I make that into a collection class I'm having trouble understanding this. I need to derive it from the CObList in MFC. Thanks for your time guys. -CDudd:)

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Why do you need to write your own collection ? What are you making a container of ? I guess pointers to some MFC type ? There seem to be some examples in MSDN for this, go to msdn.microsoft.com if you don't have MSDN on your PC. Personally, I'd use std::vector, but anyone know knows me remotely knew I'd say that. I'm not familiar with any benefits of CObList, but if it's a container specialised for MFC types, there may well be some, in which case I can't help too much except recommending you check the MSDN. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002

      1 Reply Last reply
      0
      • C CDuddley

        Say I have an array Stuff[100] how would I make that into a collection class I'm having trouble understanding this. I need to derive it from the CObList in MFC. Thanks for your time guys. -CDudd:)

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

        If what you mean is how to have a CObList of 100 elements, you would have to insert them one by one using CObList::AddTail. The following restrictrions apply:

        1. What you store are not CObjects, but pointers to CObject created with new (CObList assumes responsibility for deleteing them later). In particular, you cannot store simple types like ints or structs not deriving from CObject.
        2. When you retrieve an object stored in a CObList (well, a pointer to it), you have to figure out its actual type and downcast (most usually what you store are not CObjects but some other class derive from this type.)

        If you don't mind me evangelizing just a little about STL, why don't you give std::vector or std::list a try? They are way more robust than MFC containers, platform-independent and a lot of fun :) Plus, Christian Grauss is writing a series of tutorials on STL to help newbies make a warm start. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

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

          If what you mean is how to have a CObList of 100 elements, you would have to insert them one by one using CObList::AddTail. The following restrictrions apply:

          1. What you store are not CObjects, but pointers to CObject created with new (CObList assumes responsibility for deleteing them later). In particular, you cannot store simple types like ints or structs not deriving from CObject.
          2. When you retrieve an object stored in a CObList (well, a pointer to it), you have to figure out its actual type and downcast (most usually what you store are not CObjects but some other class derive from this type.)

          If you don't mind me evangelizing just a little about STL, why don't you give std::vector or std::list a try? They are way more robust than MFC containers, platform-independent and a lot of fun :) Plus, Christian Grauss is writing a series of tutorials on STL to help newbies make a warm start. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          C Offline
          C Offline
          CDuddley
          wrote on last edited by
          #4

          Well for a class to use the CObList it has to be derived from CObList. So, if I made a CStuff class how would I derive and, use it to make it like an array? -CDudd I don't know how to use STL so that is why I don't use it. :)

          J 1 Reply Last reply
          0
          • C CDuddley

            Say I have an array Stuff[100] how would I make that into a collection class I'm having trouble understanding this. I need to derive it from the CObList in MFC. Thanks for your time guys. -CDudd:)

            T Offline
            T Offline
            Tim Smith
            wrote on last edited by
            #5

            Ditto on the STL stuff. I might have some strong reservations about many of the STL features, but the containers are GOLDEN... droooool... Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

            1 Reply Last reply
            0
            • C CDuddley

              Well for a class to use the CObList it has to be derived from CObList. So, if I made a CStuff class how would I derive and, use it to make it like an array? -CDudd I don't know how to use STL so that is why I don't use it. :)

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

              Maybe I'm misunderstanding you here. What are you trying to do?

              1. Create a list of 100 CStuff objects.
              2. Define CStuff to behave as a CObist of fixed size (100).
              3. Other.

              I don't know how to use STL so that is why I don't use it. Don't mean to be offensive, but seemingly you don't know how to use CobList either and yet you're trying to. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

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

                Maybe I'm misunderstanding you here. What are you trying to do?

                1. Create a list of 100 CStuff objects.
                2. Define CStuff to behave as a CObist of fixed size (100).
                3. Other.

                I don't know how to use STL so that is why I don't use it. Don't mean to be offensive, but seemingly you don't know how to use CobList either and yet you're trying to. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                C Offline
                C Offline
                CDuddley
                wrote on last edited by
                #7

                I need a CStuff class to use all the functions of CObList so I can create a list of 100 CStuff objects. -CDudd No offense taken, your right I don't quite understand CObList...yet, but it's just one class others I know how to use. Since I don't know anything at all about STL I don't use it. ;)

                J 1 Reply Last reply
                0
                • ? Guest
                  C Offline
                  C Offline
                  CDuddley
                  wrote on last edited by
                  #8

                  Sorry I meant to create 100 Stuffs not CStuffs. -CDudd:-O

                  1 Reply Last reply
                  0
                  • C CDuddley

                    I need a CStuff class to use all the functions of CObList so I can create a list of 100 CStuff objects. -CDudd No offense taken, your right I don't quite understand CObList...yet, but it's just one class others I know how to use. Since I don't know anything at all about STL I don't use it. ;)

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

                    If I'm getting your point now, what you need to do is having CStuff derive from CObject:

                    class CStuff: public CObject
                    {
                    ... // as before
                    };

                    Now say your list of CStuffs is called (in a fit of originality) stuffList. So, to enter a new object (up to 100, if that's your wish) you just write:

                    stuffList.AddTail(new CStuff(/*args here if ctor needs them */));

                    Hope this is what you were after. Please tell us if it is not. As for the STL stuff, do not be frightened by its rather awesome aspect. In fact, having a std::list of CStuffs is no harder than with CObList (and you won't have to derive from CObject):

                    std::list<CStuff> stuffList;
                    ...
                    stuffList.push_back(CStuff()); // no new here, we store objects rather than pointers to them

                    Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

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

                      If I'm getting your point now, what you need to do is having CStuff derive from CObject:

                      class CStuff: public CObject
                      {
                      ... // as before
                      };

                      Now say your list of CStuffs is called (in a fit of originality) stuffList. So, to enter a new object (up to 100, if that's your wish) you just write:

                      stuffList.AddTail(new CStuff(/*args here if ctor needs them */));

                      Hope this is what you were after. Please tell us if it is not. As for the STL stuff, do not be frightened by its rather awesome aspect. In fact, having a std::list of CStuffs is no harder than with CObList (and you won't have to derive from CObject):

                      std::list<CStuff> stuffList;
                      ...
                      stuffList.push_back(CStuff()); // no new here, we store objects rather than pointers to them

                      Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                      C Offline
                      C Offline
                      CDuddley
                      wrote on last edited by
                      #10

                      I've been trying for about an hour or so and I'm royally stuck. Basically I've got a class CShapePt. The program is like a mini paint utility. Whenever the user clicks on the client area it takes the point clicked. Then the next time he/she clicks it draws a line between the two places. The Array it was using was in the MainFrame Header file. I'm not exactally sure how to put the CShapePt object into the MainFrame header file so that the device context can access it. How do you do those code highlighter things? If you need some code to help you see what I'm having trouble explaining I'll be glad to put some snippets on here. Thanks. -CDudd

                      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