C + + copy constructor Tamper-proof example _c language

Source: Internet
Author: User
Tags shallow copy

For ordinary types of objects, replication between them is simple, such as:

Copy Code code as follows:

int a = 88;
int b = A;

But class and ordinary object are different, the internal structure of class object is generally more complex, there are various member variables.

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 B and completes the copy process of object A.

In the case of objects, objects of the same type are copied from the constructor to complete the entire replication process.

Copy Code code as follows:

Cexample (const cexample& C)
{
A=C.A;
}

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;
}

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.