A simple introduction to C + + deep copy

Source: Internet
Author: User
Tags shallow copy

For a general object such as

int a = 10;
int B = 20;

Assignment between objects, the copying process is very simple, but for the class object, there are various types of variables inside, the copy process is more complex. In fact, during the object copy process. If you do not have a custom copy constructor, the system provides a default copy constructor, the default copy constructor for the base type of the member variable, in bytes, and for the member variable of the class type, the corresponding copy constructor is called, the prototype is as follows:

String (const string& that) {}

This is not a problem for a generic type variable copy, but if a variable with a pointer type in the class uses the system's default copy constructor, the problem arises because the default copy constructor is copied by byte, and for pointer-type member variables, only the pointer itself is copied, not the pointer to the object this is called a shallow copy, A shallow copy causes a problem where the pointer is repeatedly released, and a break error occurs during the run phase. such as the following code

#include <iostream>
#include <cstring>
using namespace Std;
Class String {
Public
String (const char* psz=null): M_psz (strcpy (New Char[strlen (psz?psz: ") +1], (psz?psz:" "))) {
cout << "string construction" << Endl;
}
~string () {
if (m_psz) {
Delete[] M_psz;
M_psz = NULL;
}
cout << "string destructor" << Endl;
}
char* c_str (void) {
return m_psz;
}
Private
char* M_psz;
};
int main (void) {
String S1 ("Hello");
String S2 (S1);
cout << "S1" << s1.c_str () << Endl;
cout << "S2" << s2.c_str () << Endl;
S1.C_STR () [0] = ' H ';
cout << "S1" << s1.c_str () << Endl;
cout << "S2" << s2.c_str () << Endl;
return 0;
}
In the code above, the M_psz pointer in the S1,S2 object points to the same piece of memory area "Hello", so the first character in the memory area of M_psz point in S1 is changed to uppercase "H", the S2 output is also capitalized "H", and, more importantly, when the object is released, The memory is freed two times to cause the program to crash.
Add our own defined copy constructor to the program as follows
String (const string& that): M_psz (strcpy ((New Char[strlen (THAT.M_PSZ) +1]), That.m_psz) {
cout << "string copy construction" << Endl;
}
It's no problem to run again.
A copy constructor is used when a function has a parameter of an object type or a return value of an object type.

The copy assignment is basically the same as copy copy. Just copy assignment is an operator overload problem.
We can customize the copy assignment function as follows:
void operator= (const string& that) {
M_psz = strcpy (new Char[strlen (THAT.M_PSZ) +1],that.m_psz);
}

There are too few questions to consider, and we know that for normal variables a=b returns a reference to Lvalue a, so it can continue to receive other values (a=b) = 30 as an lvalue, so that our operator overloads return a reference to the class object (otherwise the return value will not be evaluated as an lvalue)
So the following implementation is a common approach
string& operator= (cosnt string& that) {
if (&that! = this) {
Char *psz = strcpy (new Char[strlen (THAT.M_PSZ) +1],that.m_psz);//If failure throws an exception, M_psz finally releases it in a destructor
Delete[] M_psz;
M_psz = Psz;
}
return *this;
}


A simple introduction to C + + deep 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.