C + + Copy constructors (deep copy, shallow copy)--Go

Source: Internet
Author: User
Tags shallow copy

Tag: The function returns the ACE copy constructor size class type HTTPS log C + +

Copy constructor:

A copy constructor is a special constructor, and the name of the function must be the same as the class name, and its only parameter is a reference variable of this class, which is a const type, immutable. For example, the copy constructor for Class A is in the form of a (a& x). When you initialize another newly constructed object with a custom class type Object that has already been initialized, the copy function is automatically called. In other words, the copy constructor is called when the object of the class needs to be copied. The copy constructor is automatically called in the following situations:

1. An object passed into the function body in the way of value passing

2. An object is returned from the function as a value, as follows:

#include <iostream>using namespacestd;classb{ Public: B () {cout<<"Constructor B"<<Endl;} B (inti):d ata (i) {cout<<"Constructor B"<<" "<<data<<Endl;} b Play (b b) {returnb;} ~b () {cout<<"destructor b"<<Endl;}Private:    intdata;};intMain () {B temp; Temp.play (5); return 0;}
View Code

3. One object needs to be initialized by another object

If a copy constructor is declared without a display in the class, the compiler automatically generates a default copy constructor that completes the copy between objects. Custom copy constructors are a good programming style that prevents the compiler from forming a default copy constructor, which improves the efficiency of the source code.

Deep copy and shallow copy:

In some cases, in-class member variables need to dynamically open up heap memory, if a bit copy (bit copy is also called a shallow copy), that is, the value inside the object is completely copied to another object, such as A=b. At this point, if a member variable pointer in B has already applied for memory, that member in a also points to the same piece of memory. This is the problem: the B memory is freed, a pointer in A is a wild pointer, there is a run error.

Deep copy and shallow copy can be simply understood as: If a class has resources, when the object of this class has a replication process, the resource is redistributed, the process is a deep copy, and the other is not redistributed resources is a shallow copy.

Shallow copy:

#include <iostream>#include<string.h>using namespacestd;classexample{Private:    intA; Char*str; Public: Example () {cout<<"Call Constructor"<<Endl; }    ~Example () {cout<<"Call destructor"<<Endl; } Example (intB) {a=b;
cout<< "Call constructor" <<endl;
    }    Example (const example& Example)// Custom copy constructor     {        A =example.a;     }    void  Show ()    {        cout<<a<<Endl;    }}; int Main () {    Example A1 (ten);    Example a2=A1;    A2.show ();     return 0 ;}

Output:

Call constructor

10

Call destructor

  Call destructor

Deep copy:

#include <iostream>#include<string.h>using namespacestd;classexample{Private:    intA; Char*str; Public: Example () {cout<<"Call Constructor"<<Endl; }    ~Example () {Deletestr; cout<<"Call destructor"<<Endl; } Example (intBChar*e_str) {a=b; STR=New Char[b]; strcpy (STR,E_STR);
cout<< "Call constructor" <<endl;
} Example (Constexample& Example)//Custom Copy Constructors{a=Example.a; STR=New Char[A];//Deep Copy        if(str!=0) strcpy (STR,EXAMPLE.STR); }    voidShow () {cout<<str<<Endl; }};intMain () {Example A1 (Ten,"Hello"); Example A2=A1;    A2.show (); return 0;}

Output:

Call constructor

Hello

Call destructor

Call destructor

Reference Address: https://www.cnblogs.com/BlueTzar/articles/1223313.html

C + + copy constructors (deep copy, shallow copy)--Go

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.