18. c ++ primer 4th notes, replication Control

Source: Internet
Author: User

1, Explicit Constructor

A copy constructor is a special constructor with a single parameter.ConstIs a reference to this type.. When you define a new object and initialize it with an object of the same type, the copy constructor is explicitly used. When an object of this type is passed to a function or function to return an object of this type, the copy constructor is implicitly used.

Non-negative about automatic compiler executionStaticData member destructor.

2,The value assignment operator can be overloaded by specifying different types of right operands..

3, There is a special common situation that requires custom replication control members: the class has pointer members.

4,C ++Two initialization modes are supported: Direct initialization and copy initialization. Replication Initialization=,The initialization is placed in parentheses during direct initialization.

When it is used for class type objects, the initialization replication form and direct form are different:Direct initialization directly calls the constructors that match the real parameters, and the copy constructor is always called during replication initialization.During replication initialization, you first create a temporary object using the specified constructor, and then use the copy constructor to copy the temporary object to the object being created:

Example

 
String null_book = "00000"; // copy the constructor
String DOTC (10, '.'); // directly initialize
String empty_copy = string (); // copy the constructor
String empty_direct; // Initialization

5, When a non-reference type is used as the return valueReturnStatement.

6, The synthesis of the copy constructor

If we do not define a replication constructor, the compiler will synthesize one for us. The merging and copying constructor performs member initialization one by one to initialize the new object as a copy of the original object.

The so-called "members one by one ",Indicates that the compiler converts each non-StaticMembers, and copy them to the created objects in sequence.There is only one exception. The type of each member determines the meaning of the member to be copied. The composite replication constructor directly copies the values of built-in type members. class members use the replication constructor of this class to copy the values. Copying an array member is an exception. Although arrays cannot be copied, if a class has an array member, the composite copy constructor will copy the array. When copying an array, the synthesis and replication constructor copies each element of the array.

7Most classes should define the copy constructor and default constructor.Class objects that cannot be copied can only be passed as references to functions or returned from functions. They cannot be used as container elements.The default constructor is merged only when no other constructor exists.

8The built-in value assignment operation returns a reference to the left operand.

Example

 
Class sales_item
{
Public:
Sales_item & operator = (const sales_item & RHs)
{
ISBN = RHS. ISBN;
Units_sold = RHS. units_sold;
Revenue = RHS. revenue;
Return * This; // return the reference of the left operand.
}
};

9The merging assignment operator is similar to the merging copy constructor. Assign values to members one by one: each Member of the right operand object is assigned to the corresponding member of the left operand object. In addition to arrays, each member is assigned a value in the general method of its type. Assign values to each array element to an array.

10, Destructor

When the reference or pointer of an object exceeds the scope, the Destructor is not executed. Only when the pointer or actual object (not referenced by the object) pointing to the dynamically assigned object is deleted out of scope will the Destructor be run.

If a class requires a destructor, it also requires a value assignment operation and a copy constructor. This is a useful empirical rule (usually called the three-rule ).

11Unlike the copy constructor or value assignment operator, the compiler will always synthesize an destructor.The compositing destructor revokes each non-StaticMember. The compositing destructor does not delete the objects pointed to by pointer members.

12The Destructor does not have any form parameters and cannot be overloaded.Even if we write our own destructor, the synthesis destructor still runs.(First run the custom, then run the merged)

13,The Destructor does not affect Member revocation of the built-in type or pointer type. The value assignment operation must be a member of the class and return a reference to the class object.

For details about the deep copy and shallow copy operations involved in the copy constructor and their similarities and differences with the value assignment operation, refer to the followingArticle.

Reference

[1]Http://blog.163.com/zhoumhan_0351/blog/static/3995422720100250413207/

[2]Http://blog.163.com/zhoumhan_0351/blog/static/39954227201032845132592/

[3]Http://blog.163.com/zhoumhan_0351/blog/static/399542272010318112048522/

[4]Http://blog.163.com/zhoumhan_0351/blog/static/39954227201032092854732/

[5]Http://blog.163.com/zhoumhan_0351/blog/static/3995422720100284731826/

[6]Http://blog.163.com/zhoumhan_0351/blog/static/39954227201012465955824/

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.