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 thread follow up

Inheritance and arrays thread follow up

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structuresoophelptutorial
4 Posts 3 Posters 9 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

    I`m trying to place a derived class object into an c++ array containing base class objects. You suggested I should use the following

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

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

    The code above doesn`t compile with VS2022. This compiles:

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

    I can't find a solution for the following LUnits[0] = dynamic_cast(S); The compiler doesn't recognize the = operator. The errors are E0349 no operator "=" matches these operands and Error C2679 binary '=': no operator found which takes a right-hand operand of type 'unit *' (or there is no acceptable conversion) Any ideas how to move forward?

    M K 2 Replies Last reply
    0
    • C Calin Negru

      I`m trying to place a derived class object into an c++ array containing base class objects. You suggested I should use the following

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

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

      The code above doesn`t compile with VS2022. This compiles:

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

      I can't find a solution for the following LUnits[0] = dynamic_cast(S); The compiler doesn't recognize the = operator. The errors are E0349 no operator "=" matches these operands and Error C2679 binary '=': no operator found which takes a right-hand operand of type 'unit *' (or there is no acceptable conversion) Any ideas how to move forward?

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

      I think you are missing an indirection level:

      unit* LUnits = new unit[2];

      Lunis is a normal array. Elements are unit objects

      LUnits[0] = dynamic_cast(S);

      This fails because LUnits[0] is a unit, not a unit* The first line should be:

      unit** LUnits = new unit*[2];

      Mircea

      1 Reply Last reply
      0
      • C Calin Negru

        I`m trying to place a derived class object into an c++ array containing base class objects. You suggested I should use the following

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

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

        The code above doesn`t compile with VS2022. This compiles:

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

        I can't find a solution for the following LUnits[0] = dynamic_cast(S); The compiler doesn't recognize the = operator. The errors are E0349 no operator "=" matches these operands and Error C2679 binary '=': no operator found which takes a right-hand operand of type 'unit *' (or there is no acceptable conversion) Any ideas how to move forward?

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

        unit* LUnits = new units[2] creates an array of 2 unit, not an array of 2 pointers to unit. You probably want a double dereference:

        unit** LUnits = new units*[2]; // create an array of pointers to unit
        soldier* S = new soldier;
        unit[0] = S; // nb we don't need to down cast from soldier to unit

        It feels like there should be a better way to do that.

        "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

        C 1 Reply Last reply
        0
        • K k5054

          unit* LUnits = new units[2] creates an array of 2 unit, not an array of 2 pointers to unit. You probably want a double dereference:

          unit** LUnits = new units*[2]; // create an array of pointers to unit
          soldier* S = new soldier;
          unit[0] = S; // nb we don't need to down cast from soldier to unit

          It feels like there should be a better way to do that.

          "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

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

          Thank you guys indirection was the problem

          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