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. C / C++ / MFC
  4. managing objects derived from the same base class using a container

managing objects derived from the same base class using a container

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsdockerdata-structuresquestion
16 Posts 5 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.
  • Greg UtasG Greg Utas

    Yes. If objects belong to different classes, the only way to store them in a container is to use pointers, because the container allocates the same amount of memory for each entry. If you also want the container to delete an entry when you erase it, declare it as, for example, vector>.

    Robust Services Core | Software Techniques for Lemmings | Articles
    The fox knows many things, but the hedgehog knows one big thing.

    C Offline
    C Offline
    Calin Negru
    wrote on last edited by
    #6

    Quote:

    because the container allocates the same amount of memory for each entry

    that is an interesting fact

    1 Reply Last reply
    0
    • C Calin Negru

      thanks, that helps.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #7

      You are welcome. I also think @Mircea-Neacsu's advice about unique_ptr is well worth taking.

      C 1 Reply Last reply
      0
      • L Lost User

        You are welcome. I also think @Mircea-Neacsu's advice about unique_ptr is well worth taking.

        C Offline
        C Offline
        Calin Negru
        wrote on last edited by
        #8

        pointers from a library, that`s a topic that`s a bit too advanced or complicated for my present day understanding.

        K L 2 Replies Last reply
        0
        • C Calin Negru

          pointers from a library, that`s a topic that`s a bit too advanced or complicated for my present day understanding.

          K Offline
          K Offline
          k5054
          wrote on last edited by
          #9

          If you can handle raw pointers, then C++ smart pointers should be easy to understand. Google for C++ unique_pointer tutorial and read through a few of the returned hits. It's fairly straight forward, and in general new C++ development should use the smart pointers instead of using raw (e.g. new/delete).

          Keep Calm and Carry On

          C 1 Reply Last reply
          0
          • C Calin Negru

            pointers from a library, that`s a topic that`s a bit too advanced or complicated for my present day understanding.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #10

            It's a lot simpler than classes and inheritance. :laugh:

            C 1 Reply Last reply
            0
            • K k5054

              If you can handle raw pointers, then C++ smart pointers should be easy to understand. Google for C++ unique_pointer tutorial and read through a few of the returned hits. It's fairly straight forward, and in general new C++ development should use the smart pointers instead of using raw (e.g. new/delete).

              Keep Calm and Carry On

              C Offline
              C Offline
              Calin Negru
              wrote on last edited by
              #11

              The problem is I hate complicated syntax. Containers are already complicated syntax for me, combine that with another object (pointer) from a library and it becomes unintelligible mess. I will use a complicated feature when I really need to use it and there is no other way around it. Usually I need to use a feature a couple months before I can move on to something more complicated.

              1 Reply Last reply
              0
              • L Lost User

                It's a lot simpler than classes and inheritance. :laugh:

                C Offline
                C Offline
                Calin Negru
                wrote on last edited by
                #12

                Richard you really think so?

                L 1 Reply Last reply
                0
                • C Calin Negru

                  Richard you really think so?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #13

                  Did you notice the :laugh: icon?

                  C 1 Reply Last reply
                  0
                  • L Lost User

                    Did you notice the :laugh: icon?

                    C Offline
                    C Offline
                    Calin Negru
                    wrote on last edited by
                    #14

                    ok, it was a joke

                    1 Reply Last reply
                    0
                    • L Lost User

                      Yes, except for: 1. Why are you using an array size reference in your instantiations? All you should need is:

                      derivedclass * Derived1 = new derivedclass();

                      2. Your container is defined to use somebaseclass * types, so your calls to push_back should be:

                      AllObject->push_back((somebaseclass*)Derived1);

                      or better still, using proper C++ casts:

                      AllObject->push_back(reinterpret_cast(Derived1));

                      C Offline
                      C Offline
                      Calin Negru
                      wrote on last edited by
                      #15

                      I guess converting the object(pointer) back to it`s original form when time comes to use it somewhere works the same

                      derivedclass * DerClpointer1 = (derivedclass *)AllObjects->at(0);

                      Is there a way to check is the conversion is valid? like if an object is of a certain type. For instance how do I convert all objects to their derived state type in a for loop?

                      for(int i =0; i< AllObjects->size();i++ )
                      {
                      // if AllObjects->at(i) is of type derivedclass covert to derivedclass
                      }

                      L 1 Reply Last reply
                      0
                      • C Calin Negru

                        I guess converting the object(pointer) back to it`s original form when time comes to use it somewhere works the same

                        derivedclass * DerClpointer1 = (derivedclass *)AllObjects->at(0);

                        Is there a way to check is the conversion is valid? like if an object is of a certain type. For instance how do I convert all objects to their derived state type in a for loop?

                        for(int i =0; i< AllObjects->size();i++ )
                        {
                        // if AllObjects->at(i) is of type derivedclass covert to derivedclass
                        }

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #16

                        Calin Cali wrote:

                        guess converting the object(pointer) back to it`s original form when time comes to use it somewhere works the same

                        Yes it should do, but you need to run some tests to make sure.

                        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