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. Inheritance and arrays

Inheritance and arrays

Scheduled Pinned Locked Moved C / C++ / MFC
c++dockerdata-structuresoophelp
8 Posts 3 Posters 13 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
    Calin Negru
    wrote on last edited by
    #1

    Let`s say I have a cocktail of objects of different type but having the same base class. Would it be possible to store the cocktail into a C++ array rather then a stl container?

    class soldier : public unit
    {
    public:
    void RunSM1();
    };

    unit* LUnits;
    int lunitcount = 2;

    int main()
    {
    LUnits = new unit[lunitcount];
    LUnits[0].Init();
    soldier * S = new soldier();
    &LUnits[1] = S; // compiler error
    LUnits[1] = *S; // no compiler error
    return 0;
    }

    M L 2 Replies Last reply
    0
    • C Calin Negru

      Let`s say I have a cocktail of objects of different type but having the same base class. Would it be possible to store the cocktail into a C++ array rather then a stl container?

      class soldier : public unit
      {
      public:
      void RunSM1();
      };

      unit* LUnits;
      int lunitcount = 2;

      int main()
      {
      LUnits = new unit[lunitcount];
      LUnits[0].Init();
      soldier * S = new soldier();
      &LUnits[1] = S; // compiler error
      LUnits[1] = *S; // no compiler error
      return 0;
      }

      M Offline
      M Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      Yes, it is possible. Moreover, it is very common to do that. When you access the objects you can convert a pointer to base to the derived type using either dynamic_cast or a specialized type function:

      Soldier *s = dynamic_cast(LUnits[0]);
      if (!s)
      printf("Element 0 is not a soldier\n");
      s = dynamic_cast(LUnits[1]);
      if (s)
      printf ("Element 1 is indeed a soldier\n");

      Mircea

      C 1 Reply Last reply
      0
      • C Calin Negru

        Let`s say I have a cocktail of objects of different type but having the same base class. Would it be possible to store the cocktail into a C++ array rather then a stl container?

        class soldier : public unit
        {
        public:
        void RunSM1();
        };

        unit* LUnits;
        int lunitcount = 2;

        int main()
        {
        LUnits = new unit[lunitcount];
        LUnits[0].Init();
        soldier * S = new soldier();
        &LUnits[1] = S; // compiler error
        LUnits[1] = *S; // no compiler error
        return 0;
        }

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

        Yes, it is possible with correct casting, if you are just storing pointers. But you still need to know what type each entry is next time you refer to them. Also the line &LUnits[1] = S; is incorrect, you do not need the leading & character. So you can store them with something like:

        soldier * S = new soldier();
        LUnits[0] = dynamic_cast(S);
        sailor* AB = new sailor();
        LUnits[1] = dynamic_cast(AB);

        // and then sometime later

        sailor* tar = dynamic_cast(LUnits[1]);

        // but if you do
        soldier* squaddie = dynamic_cast(LUnits[1]);
        // you have a pointer that is incorrect

        [edit] @Mircea-Neacsu provides a clearer explanation above. [/edit]

        M C 2 Replies Last reply
        0
        • L Lost User

          Yes, it is possible with correct casting, if you are just storing pointers. But you still need to know what type each entry is next time you refer to them. Also the line &LUnits[1] = S; is incorrect, you do not need the leading & character. So you can store them with something like:

          soldier * S = new soldier();
          LUnits[0] = dynamic_cast(S);
          sailor* AB = new sailor();
          LUnits[1] = dynamic_cast(AB);

          // and then sometime later

          sailor* tar = dynamic_cast(LUnits[1]);

          // but if you do
          soldier* squaddie = dynamic_cast(LUnits[1]);
          // you have a pointer that is incorrect

          [edit] @Mircea-Neacsu provides a clearer explanation above. [/edit]

          M Offline
          M Offline
          Mircea Neacsu
          wrote on last edited by
          #4

          Let me respectfully comment:

          soldier * S = new soldier();
          //LUnits[0] = dynamic_cast(S);
          //no need for 'dynamic_cast' a pointer to derived can always be converted to pointer to base so:
          LUnit[0] = S;

          sailor* AB = new sailor();
          //LUnits[1] = dynamic_cast(AB); //same argument
          LUnit[1] = AB;

          // and then sometime later

          sailor* tar = dynamic_cast(LUnits[1]);

          // but if you do
          soldier* squaddie = dynamic_cast(LUnits[1]);
          // you have a pointer that is incorrect
          // you have a NULL pointer. This is the standard conforming behaviour.

          For details see: dynamic_cast conversion - cppreference.com[^]

          Mircea

          L 1 Reply Last reply
          0
          • M Mircea Neacsu

            Let me respectfully comment:

            soldier * S = new soldier();
            //LUnits[0] = dynamic_cast(S);
            //no need for 'dynamic_cast' a pointer to derived can always be converted to pointer to base so:
            LUnit[0] = S;

            sailor* AB = new sailor();
            //LUnits[1] = dynamic_cast(AB); //same argument
            LUnit[1] = AB;

            // and then sometime later

            sailor* tar = dynamic_cast(LUnits[1]);

            // but if you do
            soldier* squaddie = dynamic_cast(LUnits[1]);
            // you have a pointer that is incorrect
            // you have a NULL pointer. This is the standard conforming behaviour.

            For details see: dynamic_cast conversion - cppreference.com[^]

            Mircea

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

            Thank you, I was working from memory instead of reviewing some code I wrote years ago.

            1 Reply Last reply
            0
            • M Mircea Neacsu

              Yes, it is possible. Moreover, it is very common to do that. When you access the objects you can convert a pointer to base to the derived type using either dynamic_cast or a specialized type function:

              Soldier *s = dynamic_cast(LUnits[0]);
              if (!s)
              printf("Element 0 is not a soldier\n");
              s = dynamic_cast(LUnits[1]);
              if (s)
              printf ("Element 1 is indeed a soldier\n");

              Mircea

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

              Thank you

              1 Reply Last reply
              0
              • L Lost User

                Yes, it is possible with correct casting, if you are just storing pointers. But you still need to know what type each entry is next time you refer to them. Also the line &LUnits[1] = S; is incorrect, you do not need the leading & character. So you can store them with something like:

                soldier * S = new soldier();
                LUnits[0] = dynamic_cast(S);
                sailor* AB = new sailor();
                LUnits[1] = dynamic_cast(AB);

                // and then sometime later

                sailor* tar = dynamic_cast(LUnits[1]);

                // but if you do
                soldier* squaddie = dynamic_cast(LUnits[1]);
                // you have a pointer that is incorrect

                [edit] @Mircea-Neacsu provides a clearer explanation above. [/edit]

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

                I need to know how to execute the conversion in both directions, I find your post still useful.

                L 1 Reply Last reply
                0
                • C Calin Negru

                  I need to know how to execute the conversion in both directions, I find your post still useful.

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

                  Thank you for your comment.

                  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