C + + copy constructor (deep copy and shallow copy) detailed _c language

Source: Internet
Author: User
Tags constructor shallow copy

For ordinary types of objects, replication between them is simple, for example:
int a=88;
int b=a;
And the class object is different from the ordinary object, the internal structure of the class object is more complex and there are various member variables. Let's look at a simple example of a copy of a class object.

Copy Code code as follows:

#include <iostream>
using namespace Std;

Class Cexample {
Private
int A;
Public
cexample (int b)
{a=b;}
void Show ()
{
cout<<a<<endl;
}
};

int main ()
{
Cexample A (100);
Cexample B=a;
B.show ();
return 0;
}


Run the program, screen output 100. From the results of the above code, we can see that the system allocates memory for object B and completes the process of copying with object A. In the case of class objects, class objects of the same type are copied from the constructor to complete the entire replication process. The following example illustrates the working process of a copy constructor.
Copy Code code as follows:

#include <iostream>
using namespace Std;

Class Cexample {
Private
int A;
Public
cexample (int b)
{a=b;}

Cexample (const cexample& C)
{
A=C.A;
}
void Show ()
{
cout<<a<<endl;
}
};

int main ()
{
Cexample A (100);
Cexample B=a;
B.show ();
return 0;
}


Cexample (const cexample& C) is our custom copy constructor. As you can see, the copy constructor is a special constructor, the name of the function must be the same as the class name, and its only one argument is a reference variable of this type, which is a const type, immutable.
For example: the copy constructor of Class X is in the form of X (x& x).

The copy constructor is invoked automatically when a newly constructed object is initialized with an initialized custom class type object to initialize. That is, the copy constructor will be invoked when the object of the class needs to be copied. Copy constructors are called in the following situations:
An object is passed in as a value to the function body
An object is returned from a function in a value-passing way
An object needs to be initialized with another object.

If you do not explicitly declare a copy constructor in a class, the compiler will automatically generate a default copy constructor that completes a bit copy between objects. A bit copy is also known as a shallow copy, which is described later.

A custom copy constructor is a good programming style that prevents the compiler from forming a default copy constructor that increases the efficiency of the source code.

Shallow copy and deep copy
In some cases, a member variable in a class needs to dynamically open up heap memory, and if a bit copy is implemented, the value in 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 variable in a also points to the same memory. This is the problem: when B releases the memory (e.g., destructor), the pointer in A is a wild pointer, and a run-time error occurs.

Deep copy and shallow copy can be easily understood as: If a class has resources, when the object of this class occurs when the replication process, the resource reallocation, the process is a deep copy, conversely, no reallocation of resources, is a shallow copy. Here's an example of a deep copy.

Copy Code code as follows:

#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.A;
Str=new Char[a]; Deep copy
if (str!=0)
strcpy (STR,C.STR);
}
void Show ()
{
cout<<str<<endl;
}
~ca ()
{
Delete str;
}
Private
int A;
Char *str;
};

int main ()
{
CA A ("hello!");
CA B=a;
B.show ();
return 0;
}


The definition of deep copy and shallow copy can be easily understood as: If a class has resources (heaps, or other system resources), the process can be called a deep copy when the object of the class is replicated, whereas the object has resources, but the replication process does not replicate the resource as a shallow copy.

A shallow copy of a resource can result in a failure of the program when it frees resources.

Test &c_t is a custom copy constructor whose name must be the same as the class name, and the form parameter of the function is a reference variable of this type and must be a reference.

When initializing another newly constructed object with a custom class-type object that has already been initialized. The copy constructor is invoked automatically, and if you do not have a custom copy constructor, the system will provide a default copy constructor to complete the process. The copy core statement of the above code is accomplished through the P1=C_T.P1 statement within the constructor of the copy of Test (test &c_t).

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.