C + + copy constructor details

Source: Internet
Author: User

I. What is a copy constructor

First of all, for ordinary types of objects, the replication between them is very simple, for example:

int  - ; int

And the class object is different from ordinary object, the inner structure of class object is generally more complex, there are various member variables.
Let's look at a simple example of a class object copy.

#include <iostream>using namespacestd;classCexample {Private:intA; Public:      //constructor FunctionCexample (intb) {a=b;} //General Functions       voidShow () {cout<<a<<Endl; }};intMain () {Cexample A ( -); Cexample B= A;//Note that object initialization here calls the copy constructor, not the assignmentb.show (); return 0;}

Run the program, screen output 100. As can be seen from the running results of the above code, the system allocates memory for object B and completes the copy process with Object A. In the case of a class object, a class object of the same type is done by copying the constructor to complete the entire copy process.

The following example illustrates the working process of a copy constructor.

#include <iostream>using namespacestd;classCexample {Private:    intA; Public:    //constructor FunctionCexample (intb) {a=b;} //copy ConstructorCexample (Constcexample&C) {a=C.A; }    //General Functions    voidShow () {cout<<a<<Endl; }};intMain () {Cexample A ( -); Cexample B= A;//cexample B (A); it's the same.b.show (); return 0;} 

Cexample (const cexample& C) is our custom copy constructor. As can be seen, the copy constructor is a special constructor , and the name of the function must be the same as the class name, and it must be a reference variable of this type.

Two. Call timing for copy constructors

In C + +, the following three types of objects need to call the copy constructor!
1. Object passed in the function parameter in the way of value passing

classCexample {Private: intA; Public: //constructor FunctionCexample (intb) {a=b; cout<<"creat:"<<a<<Endl;} //Copy ConstructionCexample (Constcexample&C) {a=C.A; cout<<"Copy"<<Endl;} // Destructors~cexample () {cout<<"Delete:"<<a<<Endl;} voidShow () {cout<<a<<Endl; }};//global function, passing in an objectvoidG_fun (cexample C) {cout<<"Test"<<Endl;}intMain () {cexample Test (1); //Incoming Objectg_fun (test);return 0;}

When calling G_fun (), there are several important steps:
(1). When the. Test object passes in the parameter, a temporary variable is generated first, called C.
(2). Then call the copy constructor to give the value of test to C. The whole two steps are a bit like: cexample C (test);
(3). After the execution of the G_fun (), the C object is reconstructed.

2. The object is returned from the function in the way value is passed

classCexample {Private: intA; Public: //constructor FunctionCexample (intb) {a=b;} //Copy ConstructionCexample (Constcexample&C) {a=C.A; cout<<"Copy"<<Endl;} voidShow () {cout<<a<<Endl; }};//Global Functionscexample G_fun () {Cexample temp (0); returntemp;}intMain () {g_fun ();return 0;}

The following important steps occur when the G_fun () function executes to return:
(1). A temporary variable will be generated first, called XXXX Bar.
(2). Then call the copy constructor to give the value of temp to XXXX. The whole two steps are somewhat like: cexample XXXX (temp);
(3). At the end of the function execution, the TEMP local variable is first destructor.
(4). After the execution of the G_fun (), the XXXX object is then reconstructed.

3. Object needs to be initialized with another object

Cexample A (=//

The copy constructor is called in the latter two sentences.

C + + copy constructor details

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.