"C + + Primer" Copy control

Source: Internet
Author: User

13. Copy Control


1. Copy Constructors

The member functions in the class implicitly feel the inline type. So even if the function declaration in the class definition body is defined as the inline type, inline can be omitted when the function definition is made.


The copy constructor should be a constant reference type, assuming that agreeing to a value parameter causes an infinite loop call, which results in a memory overflow.

Copyconstruct (const copyconstruct& a) {value = A.value;}


The copy constructor can be used to initialize elements in a sequential container, such as Vector<string> Svec (5);
The default constructor and copy constructor are used in this way.

The compiler first uses the string's default constructor to create a temporary value to initialize Svec and then uses the copy constructor to copy the temporary value to each element of the Svec.



Array members are an exception, assuming the class has array members. The composite copy constructor copies the array, which copies each element of the array. Note that the pointer duplicates only the literal value.

The copy constructor is called implicitly when passing objects of that type to the function and returning the class object from the function. Therefore, the copy constructor should not be specified as explicit.

Classes that include only class types or built-in types (but not pointer types) do not need to explicitly define a copy constructor.

A copy constructor must be defined in both cases when a class has a member that is a pointer type or other resource that has members assigned in the constructor.



Some classes need to completely prohibit replication, such as the iostream class. Assuming that you want to suppress replication, the class must explicitly declare its copy constructor as private.

At this point the compiler rejects the user code, regardless of the attempt to replicate.

However, replication is also possible at this time among the members of the friend and class. Suppose you also want to suppress them and be able to declare a private copy constructor, but it is not properly defined . note is defined. Instead of defining it as an empty copy constructor.
Declaring and not defining member functions is legal. However, any attempt to use a member function without a definition will cause the link to fail. After declaring the private copy constructor, any attempt by the user code to replicate the class type object will result in a syntax error. The call to the friend and member functions will result in a link error.


If you define a copy constructor, the compiler does not synthesize the default constructor.


2. Assignment Operators

Assuming that the class does not define its own assignment operator, the compiler will synthesize one.
The return type of the assignment operator returns a reference to the left/right operand. and the assignment operator function must be a member function of the class, since the assignment must be a member of the class. The first form in the list of references is the this pointer, which is omitted .
The synthetic assignment operator function is similar to the synthetic copy constructor operation, and it also runs a member-by-value assignment.


It is generally assumed that a class needs to define a copy constructor. It will also require an assignment operator.


3. Destructors

Dynamically allocated objects are only revoked when the object's pointer is deleted.

Destructors are not executed when a reference or pointer to a dynamically allocated object is out of scope.

Destructors are called only if you explicitly call delete . But it will be undone at the end of the process.

Assuming that the class needs a destructor, it also requires assignment operators and copy constructors, which are called the Three laws.

the compiler always synthesizes a destructor for us . , the destructor of the composition is reversed in reverse order when the object is created, and each non- Static members.

For a member of a class type, the composition destructor invokes the member's destructor to undo the object.

The composition destructor does not delete the object that the pointer member points to. So it is common to define your own destructors to release those resources.

an important difference between a destructor and a copy constructor or assignment operator is: even if we write our own destructor, the composition constructor is still Execution . Destructors can only have one.

Note: The use of this is performed. The composition constructor calls the member as a destructor for the class type and is used to revoke the member.


4. Managing pointer members

Most C + + classes use one of the following three ways to manage pointer members:

1: Pointer members take regular pointer-type behavior. That is not handled.

2: Use smart pointers. Take a reference count to control the contribution object.

     Class U_ptr {         friend class Hasptr;         int *ip;         size_t use;         u_ptr (int *p): IP (P), use (1) {}         ~u_ptr () {delete IP;}     };

3: A pointing object only. Each object has its own copy.

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

"C + + Primer" Copy control

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.