This article mainly introduces the copy structure in C + +, which has a certain reference value. Interested partners can refer to it, hoping to help you
catalogue
- copy constructor
- Copy references in constructs
- When will copy constructs be used
- summary
copy constructor
Objects of the same class have exactly the same structure in memory. If copying or copying as a whole is perfectly feasible, this copying process only needs to copy data members, while function members are public (only one code); When creating an object, another object of the same class can be used to initialize the storage space of the object. At this time, the constructor used is called copy constructor
For example:
class Object { int value; public: Object(int x = 0) :value(x) {} ~Object() {} Object(Object& obj):value(obj.value) { cout << "Copy Create" << endl; } }; int main() { Object obja(10); Object objb(obja);//One object initializes another object space and calls copy construction }
When an object initializes another object space, a copy construct is called; If there is no write copy constructor in the class, the system will generate a default copy constructor just like the constructor and destructor
OBject(Object& obj) {}
Copy references in constructs
If we write a copy without a reference, it will cause dead recursion
//Object(Object& obj):value(obj.value) Object(Object obj):value(obj.value) { cout << "Copy Create" << endl; }
Why do copy constructors have to pass parameters by reference, otherwise infinite recursion will be caused?
This problem is actually very simple. There are two steps to copy objects:
Step 1: open up a temporary space;
Step 2: since the temporary space needs to be constructed, call the copy constructor again (infinite recursion...)
At the same time, we can add a reference before copying the construction parameters to limit the possible problems
//Object(Object& obj):value(obj.value) Object(const Object& obj):value(obj.value) { cout << "Copy Create" << endl; } //The const modifier here prevents us from modifying the copied object
When will copy constructs be used
Copy construction is called not only when one object is used to construct another object, but also in the following cases:
class Object{int value;public:Object(){cout << "Object::Object" << this << endl;}Object(int x = 0) :value(x){cout << "Object::Object" << this << endl;}~Object(){cout << "Objecet::~Object" << this << endl;}Object(Object& obj) :value(obj.value){cout << "Copy Create" << this << endl;}void SetValue(int x) { value = x; }int GetValue() const { return value; }};Object fun(Object obj){int val = obj.GetValue();Object obja(val);return obja;}int main(){Object objx(0);Object objy(0);objy = fun(objx);return 0;}class Object { int value; public: Object() { cout << "Object::Object" << this << endl; } Object(int x = 0) :value(x) { cout << "Object::Object" << this << endl; } ~Object() { cout << "Objecet::~Object" << this << endl; } Object(Object& obj) :value(obj.value) { cout << "Copy Create" << this << endl; } void SetValue(int x) { value = x; } int GetValue() const { return value; } }; Object fun(Object obj) { int val = obj.GetValue(); Object obja(val); return obja; } int main() { Object objx(0); Object objy(0); objy = fun(objx); return 0; }
In the above code, we created several objects in total. Let's take a look
First, ① ② call the constructor for the structure belonging to the object; The program runs to objy = fun(objx); Enter the fun function, which constructs the temporary object obj ③, which belongs to the copy structure; Then ④ construct object obja; Finally ⑤ this is also a copy structure, and a temporary object needs to be created (the value will be lost)
In addition, we cannot return the return between obja objects in the fun function to obyy, because obja will fail to destruct at the end of the function, so a new temporary object will be created here
summary
That's all for this article. I hope it can help you