Clause 12: Do not forget each part of an object when copying it.

Source: Internet
Author: User

The well-designed object-oriented system encapsulates the object internally, leaving only two functions responsible for copying the object, that is, the copy constructor and the copy assignment operator. When necessary, the compiler creates a coping function for the class and describes the "compiler generated version" Action: Copies all member variables of the Copied object.

At any time, as long as you implement the copying function of the derived class, you must carefully copy its base class components. These components are often private, so they cannot be directly accessed. Therefore, the coping function of the dispatch class should call the corresponding basic functions:

 void logCall(const string& funcName);
class Customer { public:
     ... Customer(const Customer& rhs); Customer& operator=(const Customer& rhs);
     ... private: string name; Date lastTranscation; };

  

Class PriorityCustomer: public Customer {public :... priorityCustomer (const PriorityCustomer & rhs); PriorityCustomer & operator = (const PriorityCustomer & rhs );... private: int priority;}; PriorityCustomer: PriorityCustomer (const PriorityCustomer & rhs): Customer (rhs), // call the base class's copy constructor priority (rhs. priority) {logCall ("PriorityCustomer copy constructor");} PriorityCustomer & PriorityCustomer: operator = (const PriorityCustomer & rhs) {logCall ("PriorityCustomer copy extends constructor"); Customer :: operator = (rhs); // copy the base class Customer components. priority = rhs. priority; return * this ;}

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.