#include <iostream>
using namespace std;
class Point {
public:
Point() :x(0), y(0) { cout << "Call the default constructor without parameters" << endl; }
Point(int x1, int y1) :x(x1), y(y1) { cout << "Call a constructor with parameters" << endl; }
Point(const Point &point)
{
x = point.x;
y = point.y;
cout << "Call the point copy constructor" << endl;
}
~Point() { cout << "Call destructor" << endl; }
void move(int x1, int y1) { x = x1; y = y1; }
int getX() { return x; }
int getY() { return y; }
void setX(int x1) { x = x1; }
void setY(int y1) { y = y1; }
private:
int x, y;
};
class AofPoint {
public:
AofPoint(int size) :size(size)
{
p = new Point[size];
cout << "Call afp constructor" << endl;
}
~AofPoint()//Put the release space delete[] p; in the body of the destructor, the destructor will be automatically called before the return to achieve automatic release
{
delete[] p;
cout << "Call afp destructor" << endl;
}
AofPoint(const AofPoint &afp)
{
cout << "Call afp copy constructor" << endl;
size = afp.size;
p = new Point[size];//Re-allocate a memory dynamically according to the original size
for (int i = 0; i < size;i++)//Copy the contents of the original array object
{
p[i] = afp.p[i];
}
}
Point& visit_point(int index)//Return is the object alias, take reference & to realize two-way data change, if you don’t add &, you get a copy
{
return p[index];
}
private:
Point *p;
int size;
};
int main()
{
AofPoint afp(3);//When creating an afp object, the size of the dynamic object array is also set
afp.visit_point(1).move(4,6);
afp.visit_point(2).move(8,16);
cout << "Output the contents of the first object array" << endl;
cout << "point(1).x=" << afp.visit_point(1).getX() << "\t" << "point(1).y=" << afp.visit_point(1).getY() << endl;
cout << "point(2).x=" << afp.visit_point(2).getX() << "\t" << "point(2).y=" << afp.visit_point(2).getY() << endl;
AofPoint afp2(afp);
cout << "Output the contents of the second object array" << endl;
cout << "point(1).x=" << afp2.visit_point(1).getX() << "\t" << "point(1).y=" << afp2.visit_point(1).getY() << endl;
cout << "point(2).x=" << afp2.visit_point(2).getX() << "\t" << "point(2).y=" << afp2.visit_point(2).getY() << endl;
cout << "Change the contents of the first object array to see if it affects the second object array" << endl;
afp.visit_point(1).move(8, 8);
afp.visit_point(2).move(9, 9);
cout << "Output the contents of the first object array" << endl;
cout << "point(1).x=" << afp.visit_point(1).getX() << "\t" << "point(1).y=" << afp.visit_point(1).getY() << endl;
cout << "point(2).x=" << afp.visit_point(2).getX() << "\t" << "point(2).y=" << afp.visit_point(2).getY() << endl;
cout << "Output the contents of the second object array" << endl;
cout << "point(1).x=" << afp2.visit_point(1).getX() << "\t" << "point(1).y=" << afp2.visit_point(1).getY() << endl;
cout << "point(2).x=" << afp2.visit_point(2).getX() << "\t" << "point(2).y=" << afp2.visit_point(2).getY() << endl;
cout << "If the content of the second object array is still the original, it means that the purpose of deep copying has been achieved" << endl;
return 0;
}
operation result:
Call the default constructor without parameters
Call the default constructor without parameters
Call the default constructor without parameters
Call afp constructor
Output the contents of the first object array
point(1).x=4 point(1).y=6
point(2).x=8 point(2).y=16
Call afp copy constructor
Call the default constructor without parameters
Call the default constructor without parameters
Call the default constructor without parameters
Output the contents of the second object array
point(1).x=4 point(1).y=6
point(2).x=8 point(2).y=16
Change the contents of the first object array,See if it affects the second array of objects
Output the contents of the first object array
point(1).x=8 point(1).y=8
point(2).x=9 point(2).y=9
Output the contents of the second object array
point(1).x=4 point(1).y=6
point(2).x=8 point(2).y=16
If the content of the second object array is still the original, it indicates that the purpose of deep copying has been achieved
Call destructor
Call destructor
Call destructor
Call afp destructor
Call destructor
Call destructor
Call destructor
Call afp destructor
Please press any key to continue. . .
in conclusion:
1. What are deep copy and shallow copy, and when do you need deep copy?
Deep copy: When the data member of the copied object is a pointer type, the pointer member itself is not copied, but the object pointed to by the pointer is copied.
Shallow copy: realize one-by-one copy of data between objects, and use non-pointer type members.
When the object data member is a pointer, deep copy is used, shallow copy will report an error!
such as:
AofPoint(const AofPoint &afp)
{
cout << "Call afp copy constructor" << endl;
size = afp.size;
p = afp.p;//Give the pointer of the original object array to the new object array pointer, then the two pointers point to the same address
}
Error result:
Call the default constructor without parameters
Call the default constructor without parameters
Call the default constructor without parameters
Call afp constructor
Output the contents of the first object array
point(1).x=4 point(1).y=6
point(2).x=8 point(2).y=16
Call afp copy constructor
Output the contents of the second object array
point(1).x=4 point(1).y=6
point(2).x=8 point(2).y=16
Change the contents of the first object array,See if it affects the second array of objects
Output the contents of the first object array
point(1).x=8 point(1).y=8
point(2).x=9 point(2).y=9
Output the contents of the second object array
point(1).x=8 point(1).y=8
point(2).x=9 point(2).y=9
If the content of the second object array is still the original, it indicates that the purpose of deep copying has been achieved
Call destructor
Call destructor
Call destructor
Call afp destructor
Call destructor
Call destructor
Call destructor
Call destructor
Call destructor
。。。。。
2. How to deep copy?
In the copy constructor, dynamically apply for the same size of space for the pointer again, and then copy the content of the original object, such as:
AofPoint(const AofPoint &afp)
{
cout << "Call afp copy constructor" << endl;
size = afp.size;//Shallow copy
p = new Point[size];//Re-allocate a memory dynamically according to the original size
for (int i = 0; i < size;i++)//Copy the contents of the original array object
{
p[i] = afp.p[i];
}
}
such as: High concurrency competes for shared resources. For example, spikes need to use distributed locks for shared resources such as inventory. If you do not use distributed locks, it is likely to ...
Q: Since the assignment operation referenced in java is itself atomic, why do you need AtomicReference? A: If you only need to change a reference by an assignment, you don't really need an AtomicRefer...
1 Introduction Students who know a little about .NET / C # should know the existence of the IDispose pattern, but they do not know how many students can understand this pattern thoroughly. The landlor...
The initialization list is used to initialize the data of the class. The member list is initialized in the constructor, followed by the colon of the constructor. Refer to the following example: [cpp] ...
About @RequestBody When do you need to use?@RequestBody? The value from the current end is not a complete object, just when the part of the parameters are included in the REQ, no need@RequestBody When...
If you want everyone to see the volatile keyword, but you know when you need to use the volatile keyword? Look directly below the code: When this program is compiled, if the compiler discovery program...
When do you need to rebuild an index? Indexing can bring boosts to the database in general, but the additional overhead of the index is also not hierarchically, and the reconstruction of the index is ...
When to use vuex? Only when you can't manage data well, you need to use Vuex Specifically, it is generally divided into two categories: 1. When a component needs to dispatch events multiple times The ...
Foreword When learning C ++ classes recently, it is always troubled by constructor and copy constructor. Why do you need to copy constructor? Is it possible to pass values like a general type (such ...
Bold style, what is deep copy, shallow copy Deep copy and shallow copies are for complex data types, shallow copies only copy one layer, and deep copy is a layer copy. 1. Shallow copy: Get directly to...