Copy constructors (deep copy vs shallow copy)

Source: Internet
Author: User

Copy constructors (deep copy vs shallow copy)

Initialization between class objects is completed by the copy constructor of the class. It is a special constructor that is used to initialize an object with a known object. Assume that a copy constructor is not explicitly declared in a class. The compiler will, on its own initiative, generate a default copy constructor that completes a bit copy between objects. A bit copy is also called a shallow copy.

I. Copy constructor definition Format

Class Name:: Copy constructor Name (class name & reference name)

Like what:

Tdate:: Tdate (Tdate & D); A form is a reference to an object

CString (const CString & STRINGSRC); The shape is a const object reference

Two, usually in the following three kinds of circumstances. You need to initialize the constructor with a copy:

1) Understand that there is an object initialized by an object, such as CDate day3 (D1);

2) When an object is passed to a function as a function, such as fun (Cdate day);

3) When an object is created as a return value of a function, when a temporary object is made.

Shallow copy and deep copy

A shallow copy simply assigns a copy of an object's data to a member, and in some cases, the member variable in the class needs to dynamically open up the heap memory, assuming a bit copy, that is, the value of the object is completely copied to another object, such as A=b. Then. Suppose a member variable pointer in B has already applied for memory, and that member variable in a also points to the same piece of memory. There is a problem: when B releases the memory (for example, a destructor), the pointer in a is the wild pointer, and an execution error occurs.

Deep and shallow copies can be easily understood as: Suppose a class has resources. When the object of this class occurs during the replication process. Resources are allocated again, and this process is a deep copy. Conversely, resources are not allocated again. is a shallow copy.

The following example is a deep copy.

#include "stdafx.h" #include <iostream> #include <string> #include <stdio.h>using namespace Std;class Cclass{public:cclass (char *cname= "", int snum=0), ~cclass () {cout<< "destructor class:" <<pname<<endl;delete  PName;} void Print ();p rivate:char *pname;int num;}; Cclass::cclass (char *cname,int snum) {int length = strlen (cName);p name = new Char[length+1];if (pname!=0)         //pname! =nullstrcpy (pname,cname);num=snum;cout<< "Create class:" <<PNAME<<ENDL;} void Cclass::P rint () {cout<<pname<< "The number of classes is:" <<NUM<<ENDL;} int _tmain (int argc, _tchar* argv[]) {Cclass C1 ("Computer class 061, 56); Cclass C2 (C1); C1. Print (); C2. Print (); System ("pause"); return 0;}

C1, C2 memory allocation (deep copy)

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvegf1df96ami=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Cclass (Cclass &p);  Define your own copy constructor declaration//Add Yourself Definition copy constructor Cclass::cclass (Cclass &p) {pname = new Char[strlen (p.pname) +1];if (pname!=0) strcpy ( Pname,p.pname); Num=p.num;cout<< "Create a copy of the class:" <<pname<<endl;}

Execution Result:

Create class: Computer class 061

Create a copy of the class: Computer class 061

Number of computers in 061 classes: 56

Number of computers in 061 classes: 56

Class of destruction: Computer class 061

Class of destruction: Computer class 061



Copy constructors (deep copy vs shallow copy)

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.