Item 12: Copy the object completely (copy constructor, copy operator) effective C + + notes

Source: Internet
Author: User

Item 12:copy All parts of an object

In a mature object-oriented C + + system, there are only two ways to copy objects: Copy constructors and assignment operators, which may be called copy functions . Copy functions are generated by the compiler's default function (ref: Item 5: Those functions that are silently declared and called by C + +), the default copy function does completely copy the object, but sometimes we choose to reload the copy function, the problem is here!

The implementation of a correctly copied function is this:

class Customer{  string name; Public:  Customer::Customer(Const Customer& RHS): name(RHS.name){}  Customer& Customer::operator=(Const Customer& RHS){    name = RHS.name;                     //Copy RHS ' s data    return * This;                        //See Item  }  };

It's perfect, right? But one day you added a new data member, but forgot to update the copy function:

class Customer{  string name;  Date lasttransaction; Public:  Customer::Customer(Const Customer& RHS): name(Th5.name){}  Customer& Customer::operator=(Const Customer& RHS){    name = RHS.name;                     //Copy RHS ' s data    return * This;                        //See Item  }  };

The lastTransaction compiler will not give any warning (even at the highest warning level) when you ignore it. Another common scenario is when you inherit from the parent class:

class Prioritycustomer:  Public Customer {int  Priority; Public:  Prioritycustomer::Prioritycustomer(Const Prioritycustomer& RHS)  :  Priority(RHS. Priority){}  Prioritycustomer&   Prioritycustomer::operator=(Const Prioritycustomer& RHS){     Priority = RHS. Priority;  }  };

The code above looks fine, but you forget to copy the parts of the parent class:

class Prioritycustomer:  Public Customer {int  Priority; Public:  Prioritycustomer::Prioritycustomer(Const Prioritycustomer& RHS)  : Customer(RHS),  Priority(RHS. Priority){}  Prioritycustomer&   Prioritycustomer::operator=(Const Prioritycustomer& RHS){    Customer::operator=(RHS);     Priority = RHS. Priority;  }  };

Anyway, when you implement the copy function,

    • The first thing to do is to completely copy the current object's data (local data);
    • Invokes the corresponding copy function in all parent classes.

You may have noticed the repetition of the code, but don't let the copy constructor and the assignment operator call each other, their semantics are completely different! C + + does not even provide a syntax for the assignment operator to invoke the copy constructor, which in turn allows the copy constructor call assignment operator to compile, but because the precondition of the copy constructor is an uninitialized object, and the predecessor of the assignment operator is an initialized object. Such a call is not a good design, I am afraid it will cause logic confusion.

But what about code duplication? Scott Meyers proposed can be abstracted into a common method, such as init . is not reminiscent of the objective-c init function?

Unless noted, this blog article is original, reproduced please link to the form of this article address: http://harttle.com/2015/08/01/effective-cpp-12.html

Copyright NOTICE: This article is for bloggers original articles, reproduced please attach the original link.

Item 12: Copy the object completely (copy constructor, copy operator) effective C + + notes

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.