C ++ copy constructor (deep copy, light copy) [it is difficult to write a very easy-to-understand C ++ Article] C ++ copy constructor (deep copy, light copy)

Source: Internet
Author: User

Http://www.cnblogs.com/BlueTzar/articles/1223313.html

--------------------------------------------------

C ++ copy constructor (deep copy, light copy)

For common objects, copying between them is very simple, for example:
Int A = 88;
Int B =;
Different from common objects, class objects have complicated internal structures and various member variables. The following is a simple example of copying a class object.

# Include < Iostream >
Using   Namespace STD;

Class Cexample {
Private :
Int A;
Public :
Cexample ( Int B)
{=B ;}
Void Show ()
{
Cout<A<Endl;
}
} ;

Int Main ()
{
Cexample (100);
Cexample B=A;
B. Show ();
Return 0;
}  

RunProgram, The screen output is 100. From aboveCodeThe running results show that the system allocates memory for object B and completes the replication process with object. For class objects, class objects of the same type are copied to the constructor to complete the entire replication process. The following example describes how to copy constructors.

# Include < Iostream >
Using   Namespace STD;

Class Cexample {
Private :
Int A;
Public :
Cexample ( Int B)
{=B ;}

Cexample ( Const Cexample & C)
{
A=C.;
}
Void Show ()
{
Cout<A<Endl;
}
} ;

Int Main ()
{
Cexample (100);
Cexample B=A;
B. Show ();
Return 0;
}  

Cexample(ConstCexample& C) is our custom copy constructor. It can be seen that a copy constructor is a special constructor. The name of a function must be the same as the class name. Its unique parameter is a reference variable of this type, this parameter is of the const type and is unchangeable. For example, the copy constructor of Class X is in the form of X (X & X ).

When an initialized custom class object is used to initialize another newly constructed object, the copy constructor is automatically called. That is to say, when the class object needs to be copied, the copy constructor will be called. The copy constructor is called in the following cases:
An object passes in the function body as a value.
An object is returned from the function by passing values.
An object needs to be initialized through another object.

If a copy constructor is not explicitly declared in the class, the compiler automatically generates a default copy constructor, which completes the bitwise copy between objects. Bit copy, also known as the shortest copy, will be described later.

Custom copy constructor is a good programming style. It can prevent the compiler from forming a default copy constructor and improve the source code efficiency.

Shortest copy and deep copy

In some situations, the member variables in the class need to dynamically open up the heap memory. If bit copy is implemented, the values in the object are completely copied to another
An object, such as a = B. If a member variable pointer in B has applied for memory, the member variable in a also points to the same memory. This causes a problem: when B releases the memory (for example:
In this case, the pointer in A is a wild pointer and a running error occurs.

Deep copy and shallow copy can be simply understood as: If a class has resources, when the objects of this class are copied, the resources are re-allocated. This process is a deep copy, and vice versa, if no resources are re-allocated, it is a small copy. The following is an example of deep copy.

# Include <iostream>
Using namespace STD;
Class ca
{
Public:
CA (int B, char * CSTR)
{
A = B;
STR = new char [B];
Strcpy (STR, CSTR );
}
CA (const ca & C)
{
A = C.;
STR = new char [a]; // deep copy
If (STR! = 0)
Strcpy (STR, C. Str );
}
Void show ()
{
Cout <STR <Endl;
}
~ CA ()
{
Delete STR;
}
PRIVATE:
Int;
Char * STR;
};

Int main ()
{
Ca a (10, "Hello! ");
Ca B =;
B. Show ();
Return 0;
}

The definition of deep copy and shallow copy can be simply understood as: If a class has resources (heap, or other system resources), when the object of this class is copied, this process can be called Deep copy. Otherwise, the object has resources, but the copying process does not copy resources.

A program running error occurs when resources are released after a shallow copy.

Test (test & c_t) is a custom copy constructor. The name of the copy constructor must be the same as the class name. The form parameter of the function is a reference variable of this type, it must be a reference.

When an initialized custom class object is used to initialize another newly constructed object, the copy constructor is automatically
If you do not have a custom copy constructor, the system will provide a default copy constructor to complete this process. The core statement for copying the code above is through test (test
& C_t) copy the P1 = c_t.p1 In the constructor; the statement is complete.

Related Article

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.