Here is a simple example of a copy between objects (there is no custom copy constructor in this example, and when the copy constructor is called, the compiler automatically generates a default copy constructor that completes a bit copy between objects) shallow copy :
#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;
}
Output Result:
100
The system allocates memory for object B and completes the copy process with object A. The shape of the same type object is done by copying the constructor to complete the process.
Custom constructors (custom copy constructors are a good programming style, he can organize the compiler to form a default copy constructor to improve the efficiency of the source code):
Example:
#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;
}
Output Result:
100
When an already initialized custom type Object Initializes another newly constructed object, the copy constructor 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 called as follows:
- An object passed into the function body as a value
- An object is returned from the function in the way that the value is passed
- An object needs to be initialized with another object
Shallow copy and deep copy explanation
In some cases, in-class member variables need to dynamically open up heap memory, if a bit copy is implemented, that is, 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, then the member variable in a also points to the same piece of memory. There is a problem: when B releases the memory (destructor), a memory becomes a wild pointer and a running error occurs.
Two copies can be simply understood as: If a class has resources, when the object of this class is copied, the resource is redistributed, the process of redistribution is a deep copy, conversely, there is no reallocation of resources, it is a shallow copy.
The following examples illustrate deep copies:
#include <iostream>
using namespace Std;
Class CA
{
Private
int A;
char* str;
Public
CA (int b,char* CStr)
{
A=b;
Str=new Char[b];
strcpy (STR,CSTR);
}
CA (const ca& C)
{
A=b;
Str=new Char[a];
if (str!=0)
strcpy (STR,C.STR);
}
void Show ()
{
cout<<str<<endl;
}
~ca ()
{
Delete str;
str=0;
}
};
void Main ()
{
CA A (Ten, "Hello");
CA B=a;
B.show ();
return 0;
}
Output Result:
Hello
As the above example shows, after initializing a CA object, a custom copy constructor is used to copy the variable str in a to object B, where a may call the destructor delete str operation, which causes the address of the STR variable to be recycled, If you perform a deep copy operation in a custom copy constructor, object A will cause the program to run incorrectly if it disposes of the resource and may cause the resource to be in an ambiguous state. The deep copy will re-allocate the memory unit to STR at the time of the copy, so that the variables in B can be manipulated independently without error.
C + + copy constructors (shallow copy, deep copy)