Operator= the replication operator unexpectedly

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.