"C + + Primer 15th" defines a derived class copy assignment operator

Source: Internet
Author: User

Learning materials

• The assignment operator/assignment constructor of a derived class must also handle the assignment of its base class member

Defining assignment operators

Note When copying a derived class, it is important to display the call base class copy constructor in the derived class copy constructor initialization list if you want the members of the base class to copy at the same time (of course, copying the value of the base class part in the function body is also possible, Except that it is the value of the base class member variable that is initialized with the default constructor and then modified, it is inefficient), otherwise it calls the default constructor of the base class without copying the value to the member variable of the base class, so that the resulting object, its derived class part, and the derived class part of the object being copied, The base class part is the initialization result of the default constructor.

1#include <iostream>2 using namespacestd;3 4 classA5 {6  Public:7A () {cout <<"A Default constructor"<<Endl;}8A (a&) {cout <<"A Copy Constructor"<<Endl;}9 };Ten classD | PublicA One { A  Public: -B () {cout <<"A Default constructor"<<Endl;} -B (b &b) {cout <<"B Copy Constructor"<<Endl;} the }; -  - intMain () - { + b b; -B C =b; +     return 0; A}

Output Result:

"C + + Primer 15th" defines a derived class copy assignment operator

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.