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