Inheritance and arrays thread follow up
-
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?
-
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?
I think you are missing an indirection level:
unit* LUnits = new unit[2];
Lunis
is a normal array. Elements areunit
objectsLUnits[0] = dynamic_cast(S);
This fails because
LUnits[0]
is aunit
, not aunit*
The first line should be:unit** LUnits = new unit*[2];
Mircea
-
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?
unit* LUnits = new units[2]
creates an array of 2unit
, 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 unitIt 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
-
unit* LUnits = new units[2]
creates an array of 2unit
, 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 unitIt 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
Thank you guys indirection was the problem