Inheritance and arrays
-
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;
} -
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;
}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
-
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;
}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]
-
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]
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
-
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
-
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
Thank you
-
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]
I need to know how to execute the conversion in both directions, I find your post still useful.
-
I need to know how to execute the conversion in both directions, I find your post still useful.