Valid tive C ++ (12) copies each component when copying an object

Source: Internet
Author: User

Problem focus:

Two copying operations: copy constructor and overload assignment operator. Make sure that all member variables of the Copied object are copied in one sentence.
Demo
Void logCall (const std: string & funcName); // log function class Date {...}; class Customer {public :.... customer (const Customer & rhs); Customer & operator = (const Customer & rhs); private: std: string name; Date lastTransaction;}; Customer :: customer (const Customer & rhs): name (rhs. name) // ignore the lastTransaction member variable {logCall ("Customer copy constructor");} Customer & Customer: operato = (const Customer & rhs) {logCall ("Customer copy assignment operator"); name = rhs. name; // The return * this;} variable of the lastTransaction member is ignored ;}

The problem with the above Code is obvious, that is, the lastTransaction member variable is not copied. More seriously, the compiler will not remind you of this error. The conclusion is that if you add a member variable to the class, you must modify the copying function (including the copy constructor and the heavy value assignment operator) at the same time)

What kind of danger does inheritance bring?
class PriorityCustomer: public Custoer {public:    ...    PriorityCustomer(const PriorityCustoer& rhs);    PriorityCustomer& operato=(const PriorityCustomer& rhs);    ...private:    int priority;};PriorityCustomer&PriorityCustomer::PriorityCustomer (const PriorityCustomer& rhs)    : priority(rhs.priority){    logCall("PriorityCustoer copy constructor");}PriorityCustomer&PriorityCustomer::operator=(const PriorityCustomer& rhs){    logCall("PriorityCustomer copy assignment operator");    priority = rhs.priority;    return *this;}

Problem:
The copy function of PriorityCustomer copies the member variables it declares, but ignores the copy of the member variables it inherits from the Customer, and does not specify the constructors passed to the Customer. Therefore, the Customer component of the PriorityCustomer object is initialized by the default constructor of the Customer without real parameters.

Improvement: When writing a copy function for a subclass, you must copy its base class.
PriorityCustomer&PriorityCustomer::PriorityCustomer (const PriorityCustomer& rhs)    : Customer(rsh), priority(rhs.priority){    logCall("PriorityCustoer copy constructor");}PriorityCustomer&PriorityCustomer::operator=(const PriorityCustomer& rhs){    logCall("PriorityCustomer copy assignment operator");    Customer::operator=(rhs);    priority = rhs.priority;    return *this;}

Conclusion
Make sure that all local member variables are assigned.
Call all the appropriate copy functions in base classes.

Note that:
Although the overloaded value assignment operators look very similar to the code of the copy function, it is difficult to reuse them because calling the copy constructor when the value assignment operator is overloaded is like constructing an existing object. Similarly, it makes it meaningless for the copy constructor to call the overload assignment operator.
Therefore, the method to reuse these two codes is to write a new member function to call them.

Summary:
Copy the constructor and the overload value assignment operator. Make sure to copy "all member variables in the object" and "all base class components". Do not try to copy the constructor to call the overload value assignment operator or vice versa.



References: Objective C ++ 3rd

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.