C ++ Primer (4th) Chapter13 replication Control

Source: Internet
Author: User
Document directory
  • Copy constructor
  • Destructor

Copy constructor ):A special constructor with a single parameter, which is 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 you pass an object of this type to a function or return an object of this type from a function, the copy constructor is implicitly used.

Destructor ):Constructor. When an object that exceeds the scope or is dynamically allocated is deleted, the Destructor is automatically applied. Destructor can be used to release resources acquired during object construction or during object life cycle. No matter whether the class defines its own destructor, the compiler automatically executes the destructor of non-static data members in the class.

Value assignment operator (assignment operator ):Like constructors, the value assignment operator can be overloaded by specifying different types of right operands. The version with the right operand as the class type is special: if we do not write this sort, the compiler will synthesize it for us.

 

Copy constructor

C ++ supports two initialization modes:

  • Direct initialization: place the initialization formula directly in parentheses.
  • Copy initialization: Use the = symbol.

When used for class type objects:

  • Direct initialization: directly call the constructor that matches the real parameter.
  • Copy initialization: first create a temporary object using the specified constructor, and then copy the temporary object to the object being created using the copy constructor.

 

Synthesis replication constructor: If no replication constructor is defined, the compiler will synthesize one for us. Its behavior is to execute memberwise initialize. The so-called "members one by one" means that the compiler copies each non-static member of an existing object to the newly created object in sequence. The Synthesis and replication constructor directly copies the values of built-in type members. class members use the replication constructor of this class to copy. When copying an array, each element of the array is copied.

 

To prevent replication, You can explicitly declare that the replication constructor of the class is private. However, the friends and members of the class can still be copied. If you want to prohibit the replication of the connected friends and members, you can declare a (private) replication constructor, but not define it. In this way, any attempt to use undefined members will cause the link to fail.

 

Destructor

When the reference or pointer of an object exceeds the scope, the Destructor is not allowed. Only when the pointer to the dynamically assigned object is deleted or the actual object (instead of the Object Reference) is out of scope will the Destructor be run.

When you cancel a container (whether it is a standard library container or a built-in array), it also runs the destructor of class elements in the container. Elements in the container are always revoked in reverse order.

 

Unlike the copy constructor or value assignment operator, the compiler always synthesize a destructor for us. The compositing destructor revokes each non-static member in the reverse order of the object creation. Therefore, it revokes the member in the reverse order declared by the member in the class. If the class defines the destructor, the composite constructor is run after the class-defined destructor ends.

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.