Article 12: Don't forget every ingredient when copying an object
Write a class to represent the customer, where manually writing out the copying function allows the external call record to be logged down:
#include <iostream> #include <string>using namespace std;void logcall (const string funcName) {cout<< Funcname<<endl;} Class Customer{public:customer () {}customer (const customer& RHS); customer& operator= (const customer& RHS);p rivate:string name;}; Customer::customer (const customer& RHS) {this->name = Rhs.name;logcall ("copy constructor");} customer& customer::operator= (const customer& RHS) {logcall ("assignment operator overloading"); name = Rhs.name;return *this;} int main () {Customer C1; Customer C2 (C1); Customer c3;c3 = C2;return 0;}
The above function looks fine, knowing that a new member variable is added:
#include <iostream> #include <string>using namespace std;void logcall (const string funcName) {cout<< Funcname<<endl;} Class Date{};class Customer{public:customer () {}customer (const customer& RHS); customer& operator= (const customer& RHS);p rivate:string name;date data; This adds a member variable}; The Customer::customer (const customer& RHS) {///function body does not increase the processing of the data member variable, but the compiler does not give any warning This->name = Rhs.name;logcall (" Copy constructor ");} customer& customer::operator= (const customer& RHS) {///function body does not increase the processing of data member variables, but the compiler does not give any warning Logcall ("assignment operator overloading") ; name = Rhs.name;return *this;} int main () {Customer C1; Customer C2 (C1); Customer c3;c3 = C2;return 0;} /* If you add a member variable to a class, you must also modify the copying function, and if you forget, the compiler will not give any hint. */
Note: If you add a member variable to a class, you must also modify the copying function
Now let him have the child class:
#include <iostream> #include <string>using namespace std;void logcall (const string funcName) {cout<< Funcname<<endl;} Class Date{};class Customer{public:customer () {}customer (const customer& RHS); customer& operator= (const customer& RHS);p rivate:string name;date data; This adds a member variable}; The Customer::customer (const customer& RHS) {///function body does not increase the processing of the data member variable, but the compiler does not give any warning This->name = Rhs.name;logcall (" Copy constructor ");} customer& customer::operator= (const customer& RHS) {///function body does not increase the processing of data member variables, but the compiler does not give any warning Logcall ("assignment operator overloading") ; name = Rhs.name;return *this;} Now the Inheritance class Prioritycustomer:public Customer{public:prioritycustomer (const prioritycustomer& RHS) has occurred; prioritycustomer& operator= (const prioritycustomer& RHS);p rivate:int priority;}; Prioritycustomer::P rioritycustomer (const prioritycustomer& RHS):p riority (rhs.priority) {Logcall (" Prioritycustomer copy Constructor ");} prioritycustomer& Prioritycustomer::operator = (const prioritycustomer& RHS) {LoGcall ("Prioritycustomer copy assignment Operator");p riority = Rhs.priority;return *this;} /* Prioritycustomer's copying function seems to replicate everything in Prioritycustomer, but looking closely will find that they replicate the member variables of the Prioritycustomer declaration. But each prioritycustomer also contains copies of the customer member variables (replicas) that it inherits, and these member variables are not copied. The Prioritycustome copy constructor does not have an argument passed to its base class constructor (that is, it does not refer to the customer in its list of member values), so the customer of the Prioritycustomer object The component is initialized by the customer constructor without an argument, which means that the member variable name and data within the customer will perform the default initialization. */int Main () {Customer C1; Customer C2 (C1); Customer c3;c3 = C2;return 0;} /**/
The copy function of the
subclass only copies the member variables that are added to the subclass itself, but the inherited member variable is not copied, how to solve it?
#include <iostream> #include <string>using namespace std;void logcall (const string funcName) {cout<< Funcname<<endl;} Class Date{};class Customer{public:customer () {}customer (const customer& RHS); customer& operator= (const customer& RHS);p rivate:string name;date data; This adds a member variable}; The Customer::customer (const customer& RHS) {///function body does not increase the processing of the data member variable, but the compiler does not give any warning This->name = Rhs.name;logcall (" Copy constructor ");} customer& customer::operator= (const customer& RHS) {///function body does not increase the processing of data member variables, but the compiler does not give any warning Logcall ("assignment operator overloading") ; name = Rhs.name;return *this;} /* Whenever we write a copy function for a subclass, we have to carefully copy its base class component, which is generally private, so we can't access them directly, what we can do is let the subclass's copying function call the corresponding base class function: */class prioritycustomer:public customer{public:prioritycustomer (const prioritycustomer& RHS); prioritycustomer& operator= (const prioritycustomer& RHS);p rivate:int priority;};/ /What we can do is let the copying function of the subclass call the corresponding base class function: Prioritycustomer::P rioritycustomer (const prioritycustomer& RHS): CustomeR (RHS), priority (rhs.priority)//Call base class's copy constructor {logcall ("Prioritycustomer copy Constructor");} All we can do is let the copying function of the subclass call the corresponding base class function:prioritycustomer& Prioritycustomer::operator = (const Prioritycustomer & RHS) {logcall ("Prioritycustomer copy assignment Operator"); Customer::operator = (RHS); Assign a value to the base class component priority = Rhs.priority;return *this;} int main () {Customer C1; Customer C2 (C1); Customer c3;c3 = C2;return 0;} /**/
All we can do is let the copying function of the subclass call the corresponding base class function.
Summarize:
The 1:copying function should ensure that all member variables within the object are copied and all base class components
2: Do not attempt to implement another copying function with one of the copying functions. It should be said that common functions are put into the third function and are called together by two copying functions.
Effective C + + reading notes clause 12