This question helped me sort out a little, but my question is slightly different from them.
Basic typecasting, as I understand it in C ++, involves reinterpreting a structure in memory as another structure. For instance:
class Building{int sqFootage;};
class Office : public Building{int cubicles;};
int main(int argc, char** argv){
Office *foo = new Office();
Building *bar = (Building*)foo;
return 0;
};
The critical point here is that Office can be interpreted in memory as a building without any structural changes. This breaks down in case of multiple inheritance:
class Animal{bool alive;};
class WaterDweller : public Animal{float swimSpeed;};
class LandDweller : public Animal{float runSpeed;};
class Amphibian : public WaterDweller, public LandDweller{float transitionSpeed};
int main(int argc, char** argv){
Amphibian *amph = new Amphibian();
LandDweller *land = (LandDweller*)amph;
WaterDweller *sea = (WaterDweller*)amph;
}
It is impossible to interpret amphboth a LandDwellerand a WaterDwellerwithout reorganizing the structure Amphibianin memory. How do these castes work, assuming they have some useful meaning? If not, is it possible static_cast, dynamic_castor reinterpret_cast?
EDIT: Sneftel. TinyDr, , , , c-, - , .
, , , :
land 0x22c8018
sea 0x22c8010
run 0x22c801c
swim 0x22c8014
land alive 0x22c8018
sea alive 0x22c8010
, , , , . , Sneftel .