First, look at the output of the following code:
The above code did the most natural thing. is to swap the two objects of the derived. However, the assignment output through pointers is not expected:
The call is base operator=, which means we assign the base part of D2 to D1. And now the D1 is "usually their own derived, half is D2 base" Monster!
!!
It seems that the compiler ignores our intentions, for this pointer assignment is not possible, so we need to be dynamic type, then we can be "operator=".
But through the pointer copy passed, but the general object assignment is inexplicable.
It is also necessary to add an assignment operator to the object itself. Like the following:
The result is:
OK is done. But this is just one small step. For multiple inheritance cases, there are some puzzling answers to the above code:
[To be continued]
Operator= the replication operator unexpectedly