When copying an object, do not forget its every ingredient
The author of this section reminds us that in the case of multiple inheritance of the copy or copy assignment operator=, it is important to consider the initialization of the base class partial data.
Compare the code:
Class cutsomer{...Private:stringNamestringTelphone;}; Class Prioritycustomer: Publiccutsomer{ Public:Prioritycustomer() {cout<<"Prioritycustomer Ctor"<<endl; } prioritycustomer (Constprioritycustomer& RHS):p riority (rhs.priority) {cout<<"Prioritycustomer Copy Ctor"<<endl; } prioritycustomer&operator=(Constprioritycustomer& RHS) {cout<<"Prioritycustomer Assign Operator"<<endl; priority=rhs.priority;return* This; }Private:intpriority;};
The data in Prioritycustomer has the following
int priority; string name; string telphone;
While the real copy or copy assignment only deal with the INT priority;
We can see that the above code ignores the processing of the data in the base class section, when the code is modified as follows:
PriorityCustomer(const PriorityCustomer& rhs) :Cutsomer(rhs),priority(rhs.priority) { cout<<"PriorityCustomer Copy Ctor"<<endl; } operator=(const PriorityCustomer& rhs) { cout<<"PriorityCustomer assign operator"<<endl; Cutsomer::operator=(rhs); priority=rhs.priority; return *this; }
Effective C + + clause 12