Copy constructor in C ++

Source: Internet
Author: User

1.What is a copy constructor:

Copy constructor, of course, is copying and constructing. (In fact, many names, as long as you calm down and think about it, it is really just as the name suggests) copy is also called copy, So copy constructor is also called copy constructor. In Baidu encyclopedia, copy constructor isSpecialConstructor, which consistsCompilerCall to complete someSame classOf other objectsBuild and initialization. ItsUnique Parameter(Object Reference) is immutable (Const type). This function is often usedFunction call timeUser-Defined type value transfer andReturn.

 

2.Copy constructor form

Class X

{

Public:

X ();

X (const X &);//Copy constructor

}

2.1Why is the copy construction parameter of the reference type?

The reason is as follows: when an object transmits a function by passing a value, the copy constructor is automatically called to generate an object in the function (Copy the constructor call.). If an object is passed in to its own copy constructor, its copy constructor will be called to copy the object, so that the copy can be passed in to its own copy constructor, which causesInfinite LoopStack Overflow ).

 

3.Three methods of copying constructor calls

. An object acts as a function parameter and passes in the function body as a value;

. An object acts as the function return value and is returned from the function by passing the value;

3. 3. An object is used to initialize another object (often called copy initialization ).

Summary:When an object is passed by value (whether as a function parameter or as a function return value), the compiler will first create a temporary copy of this object, when this temporary copy is created, the copy constructor of the class is called..

 

4.Deep copy and light copy

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 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.

In some cases, 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 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 (such as the destructor), the pointer in A is a wild pointer and a running error occurs. In fact, this requires deep copy. You need to customize the copy constructor.

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;

}

When resources are released after a shallow copy, the ownership of resources is unclear.ProgramAn error occurred while running.Be sure to check whether pointer members exist in the class.

 

 

5.Copy the constructor and the "=" value assignment operator

For example:

Class cexample

{};

Int main ()

{

Cexample e1 = new cexample;

Cexample e2 = e1 ;//Call the copy constructor

Cexample E3 (E1 );//Call the copy constructor

Cexample E4;

E4 = e1 ;//Call = value assignment operator

}

The general principle is:① A copy constructor should be provided for all classes that contain dynamically allocated members or pointer members. ② When providing a copy constructor, you should also consider overloading the "=" value assignment operator symbol.

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.