当前位置:网站首页>Copy constructor shallow copy and deep copy

Copy constructor shallow copy and deep copy

2022-04-23 16:44:00 Mind hacker

Catalog

copy constructor

Shallow copy

Deep copy

copy constructor

In the last installment, we talked about constructors , When it comes to constructors, there are several formal classifications , I.e. with parameters 、 Without parameters and parameter list initialization , There is also a constructor for passing references , Called a copy constructor , seeing the name of a thing one thinks of its function , Is to copy , Initialize a new class object through the relevant data of an existing class object .

We still use Point This class is explained as an example , A copy constructor is a constructor whose parameter type is a reference type .

class Point
{
	double x,y;
	public:
		Point(Point & point);
};

Shallow copy

All classes have their own copy constructors , If the programmer doesn't write a copy constructor himself , Then the system will generate a default copy constructor by default , It adopts the method of bit by bit replication to copy objects , Also known as shallow copy .

Let's write one ourselves Point Class as an example :

Point::Point(Point & point)
{
	x=point.x;
	y=point.y;
}

This is a shallow copy , Copy bit by bit . There seems to be nothing wrong , Why is it shallow ?

actually , If all data members are basic data types , There is no problem with shallow copies .

however , When the data member of a class contains a pointer , Shallow copy will have an accident .

We put Point Class change , Add a pointer to Point class , For the sake of safety , We initialize this pointer to NULL:

class Point
{
	double x,y;
    Point * p=NULL;
	public:
		Point(Point & point);
};

Then we use a light copy :

Point::Point(Point & point)
{
	x=point.x;
	y=point.y;
    p=point.p;
}

First x and y There is no problem , The pointer p Although the value of is the same , But our goal is to make the value of the pointer the same , Obviously not , You want the memory units they point to to to have the same value , If you use such a shallow copy , Then these two pointers point to the same block of memory , Some of you might say , There seems to be no problem , Didn't this copy successfully ? The problem is that once we modify one of the class objects p Point to , Then the data of another class object will also change , Because they point to the same piece of memory . therefore , In this case, we need to use deep copy .

Deep copy

Through the above analysis , We know that the problem with shallow copy is , These two pointers point to the same block of memory , So just give the generated class object a new memory space to solve the problem :

Point::Point(Point & point)
{
	x=point.x;
	y=point.y;
    if(point.p)
    {
		p=new Point;
		p->x=point.p->x;
		p->y=point.p->y;
	}
}

Here you can see , We are the first to judge p Is it a null pointer , It's not a null pointer. Let's copy , This is very important , Without this judgment , once p Is a null pointer , And we continue to operate , Cause unpredictable errors in the system .

版权声明
本文为[Mind hacker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231642096531.html