Valid tive C ++ clause 12: Do not forget every component of an object when copying it.

Source: Internet
Author: User

The copy constructor and the copy assignment operator are called copying functions. These "compiler generation version" behaviors: All member variables of objects to be baked

Make a copy.

Declare your own copying function,

Void logcall (const STD: String funcname );
Class Customer
{
Public:
Customer (const customer & RHs );
Customer & operator = (const customer & RHs );
Protected:
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;
}

Everything looks good here, and everything actually works well, knowing that another member variable is added:

Class Date {...};
Class Customer
{
Public:
Customer (const customer & RHs );
Customer & operator = (const customer & RHs );
Protected:
PRIVATE:
STD: string name;
Date lasttransaction;
};

In this case, the existing copying function is local copy: they did copy the name but did not copy the newly added lasttransaction. If you add a member variable to the class, you must modify the copying function at the same time. (You also need to modify all constructors and any non-standard operator = ).

In the event of inheritance, it may cause a hidden crisis that is the most prevalent on this topic. Considerations:

Class prioritycustomer: Public customer
{
Public:
Prioritycustomer (const prioritycustomer & RHs );
Prioritycustomer & operator = (const prioritycustomer & RHs );
Protected:
PRIVATE:
Int priority;
};

Prioritycustomer: prioritycustomer (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;
}

The copying function of prioritycustomer looks like copying everything in prioritycustomer, but prioritycustomer also contains copies of the customer member variables, but those are not copied, the copy constructor of prioritycustomer does not specify the real parameter to be passed to its base class constructor. Therefore, the customer component of the prioritycustomer object will be initialized by the default constructor without the real parameter. The default constructor performs the default initialization action on name and lasttransaction.

The base class component is often private, so you cannot directly access them. You should have the copying function of the derived class call the corresponding base class function:

Prioritycustomer: prioritycustomer (const prioritycustomer & RHs)
: Customer (RHs), // call the copy constructor of the base class
Priority (RHS. Priority)
{
Logcall ("prioritycustomer copy constructor ");
}
Prioritycustomer & prioritycustomer: Operator = (const prioritycustomer & RHs)
{
Logcall ("prioritycustomer copy assignment operator ");
Customer: Operator = (RHs); // value the base class component
Priority = RHS. priority;
Return * this;
}

When writing a copying function, make sure that 1. Copy all local member variables, 2. Call the appropriate copying function in all base classes.

When these two copying functions have similar implementation ontology, one copying function calls another copying function and you cannot achieve your desired goal.

It is unreasonable for copy assignment to call the copy constructor, because it is like attempting to construct an existing object.

The reverse direction allows the copy constructor to call the copy assignment operator, which is also meaningless. It is equivalent to assigning values to an unconstructed object.

To eliminate repeated code, create a new member function to call the two. Such functions are often private and often named as init.

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.