"Effective C + +" study notes-clause 12

Source: Internet
Author: User

*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************




Ii. Constructors,destructors and Assignment Operators


Rule 12:copy All parts of an object

Rule 12: Do not forget each ingredient when copying an object



1. Compiler's Revenge!


first, let's look at a word: the copying function, which contains: copy constructor and copy assignment operator.

Before the terms were mentioned. Assuming we don't do whatever action, the compiler will voluntarily generate the copying function for you, replicating each component in the class.

Suppose you declare these yourself. It's the equivalent of saying to the compiler that you don't need to meddle. The compiler will be "very angry" so assuming your copying function must be wrong, it will not remind you of the error, it is to see you joke!

For example, here's a sample:

void Logcall (const std::string& funcName); class Customer  {public:    ...    Customer (const customer& RHS);    customer& operator= (const customer& RHS);    ... private:    std::string name;}
Customer::customer (const customer& RHS): Name (rhs.name) {    logcall ("Customer copy constructor"); customer& customer::operator= (const customer& RHS) {    logcall ("Customer copy assignment Operator");    name = Rhs.name;    return *this;}

All of these things are normal, but suppose you add a class member variable?

Class Date  {  ...  }; Class Customer  {public:    ... <span style= "White-space:pre" ></span>//as before private:    std:: string name;    Date lasttransaction;};


This time the copying function is a local copy, followed by the data type. did not replicate.

In such a case, the angry compiler will not tell you this error, so. To add a new thing, you need to change the corresponding copying function.



2. The most prominent case--inheritance

Class Prioritycustomer:public Customer  {//a derived class
Public: ...    Prioritycustomer (const prioritycustomer& RHS);    prioritycustomer& operator= (const prioritycustomer& RHS);    ... private: int priority    ;} Prioritycustomer::P rioritycustomer (const prioritycustomer& RHS): Priority (rhs.priority) {    Logcall (" Prioritycustomer copy Constructor ");} prioritycustomer& prioritycustomer::operator= (const prioritycustomer& RHS) {    Logcall ("PriorityCustomer Copy assignment operator ";    priority = Rhs.priority;    return *this;}


This is the face. Prioritycustomer's copying function, which seems to duplicate every member of the Prioritycustomer,

But looking closely, you will find that they replicate the member variables of the Prioritycustomer declaration. Prioritycustomer is a derived class. It also includes the member variables of the base class customer, which are not copied.

This is a very serious problem and the compiler will not warn you, so assume an error. wow!

It's a big deal.


3. Pits It!

At any time, you only have to assume responsibility for writing copying functions for derived class. The composition of its base class must be copied with great care.

However, these components are often private, so they cannot be directly visited, which requires the copying function of the derived class to call the corresponding base class function:

Prioritycustomer::P rioritycustomer (const prioritycustomer& RHS): Customer (RHS), priority (Rhs.priority) {    Logcall ("Prioritycustomer copy Constructor");} prioritycustomer& prioritycustomer::operator= (const prioritycustomer& RHS) {    Logcall ("PriorityCustomer Copy assignment operator ");    Customer::operator= (RHS);    priority = Rhs.priority;    return *this;}

copy each ingredient as described in these terms. This means that when you write a copying function, make sure that:

<1> Copy all local member variables

<2> call the appropriate copying function in all base classes


Oh, that's right. There are two points to note that cannot be avoided by avoiding code repetition:

① make copy assignment operator call copy constructor

② making the copy constructor call the copy assignment operator

Usually, assuming that the code is not repeated, you can write the same code in it by creating a new private member function. It is then called by the copy assignment operator and the copy constructor.



4. Please remember

The ★copying function should ensure that all member variables within the object and all base class components are copied

★ Do not try to implement a copying function with a copying function. Common functions should be put into the third function and called together by two copying functions.





*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************

"Effective C + +" study notes-clause 12

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.