"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 every ingredient when copying an object



1. Compiler's Revenge!

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

before the terms mentioned, if we do not do any action, the compiler will automatically generate for you copying function, the class of each component is copied.

If you declare these yourself, it is equivalent to saying to the compiler that you do not need to meddle with it. The compiler will be "very angry" so if your copying function must be wrong, it will not remind you of the error, it is to see you joke!

For example, the following example:

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 what if I 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 performs a partial copy, followed by the data type, and does not replicate.

In this case, the angry compiler will not tell you the 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;}


In this, Prioritycustomer's copying function seems to duplicate every member of the Prioritycustomer,

But look closely, you will find that they replicate the member variables of the Prioritycustomer declaration, Prioritycustomer is a derived class, which also contains the base class customer's member variables, which are not copied.

This is a very serious problem and the compiler will not warn you, so if something goes wrong, wow! It's a big deal.


3. Pits It!

At any time, as long as you assume responsibility for writing copying functions for derived class. The composition of its base class must be carefully copied. However, these components are often private, so they cannot be accessed directly, 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;}

This article says copy every ingredient, which means when you write a copying function, make sure that:

<1> Copy all local member variables

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


Oh, yes, there are two more points to note, not to avoid duplication of code:

① make copy assignment operator call copy constructor

② making the copy constructor call the copy assignment operator

In general, if you are afraid to repeat the code, you can write the same code in it by creating a new private member function, and then copy the assignment operator and the copy constructor to call it.



4. Please remember

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

★ Do not attempt to implement another copying function with one of the copying functions. 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

Related Article

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.