how does c++ multiple inheritance casting work? -
helped me understand a bit, but my question is slightly different from them.
Casting the Basic Type I think that referencing a structure in memory in C ++ is, as a separate structure. For example:
class building {int sqFootage;}; Class office: public building {int cubicles;}; Int main (int argc, char ** argv) {office * foo = new office (); / * Members of the House appear on an office after a member of SQLFoothes, * therefore, Fu Pointer actually only * with some buildings * after some additional * information * / building * bar = (building *) foo; Return 0; };
The important point here is that a building can be interpreted in memory as a building without any change in the structure. It breaks into cases of multiple parts:
class animals {living alive} ;; Class Water Deluxe: Public Animal {Float Speed Speed;}; Classroom Land Deluxe: Public Animal {Float Run Speed;}; Class Amphibian: Public Water Water, Public Land Delilar (Float Transition Speed); Int main (int arc, four ** argv) {amphibian * amp = new amphibian (); Land Delilar * Land = (Landweller *) amph; Water Delaller * Sea = (Water Carrier *) amph; }
amph
is not possible to interpret both land delarrier
and water deliller
without restructuring Structure in memory amphibian
How do those briefs work, assuming that they have some useful meaning? If not, then static_cast
, dynamic_cast
, or reinterpret_cast
is appropriate here? In
Edit: Many thanks Sneftel North've made very helpful to one of your comments, and C-advised to avoid styling is something that I will take to heart.
For those who are curious, they tried the indicators when I run this code here:
Land 0x22c8018 sea 0x22c8010 run 0x22c801c swim 0x22c8014 Land Land 0x22c8018 marine life 0x22c8010
If you can see that even if land and water resident pass from the base class here, it is as if there were original class separate them including their copies. This leads to Diamond's problem described by Sneftel in his comment.
If you enter the address land
, you will see that it is amph over
can do so, because Anker arithmetic in C ++, put to an end as part of its internal operations. Amphibian
a Water Dellr
and then a Land Dellr
is when casting a base type whose data starting from the beginning of the derived class If that does not happen, then that indicator is adjusted on the basis of where the base class data starts.
Incidentaly take Dieter's advice, and do not use C-style c ++ (especially for indicative type). C-style behavior in C ++ static_cast
, reinterpret_cast
, and const_cast is a misleading of
, and for something different Compared to you, the best, to make the type of casting, you are clear, without much as a compiler warning.
Comments
Post a Comment