In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "what is c++ deep copy and shallow copy". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "what is c++ deep copy and shallow copy" can help you solve the problem.
Copy constructors are also called copy constructors;
Shallow copy is also called shallow copy or bit copy;
Deep copy is also called deep copy; shallow copy and deep copy copy are copies, creating copies. Suppose there is an object A with attributes t1, t2. Then, I copy A and get B, B should also have attributes t1, t2, and every attribute of A and B should be the same. For attribute t1 of the base type, copying is unambiguous. Simply copying a value achieves the effect of copying. For attribute t2 of the reference type, copy has two meanings. The first layer is that I just copy the address referenced by t2 to t2 of B, which does achieve the same effect of attributes, which can be understood as copying, but in fact, the attribute t2 in the two objects corresponds to the same object. Operating on the object pointed to by t2 on object B affects the value of t2 in object A. The second layer is, I take the object t2 of A points to, say o1, make a complete copy, say o2, and give t2 of B the address of the new o2. It also achieves the effect of copying, and operating on o2 pointed to by t2 of B does not affect o1 pointed to by t2 of A. The two-layer meaning of copy corresponds to the concept of shallow copy and deep copy. The first layer is shallow copy, and the second layer is deep copy.
Based on the above, it is easy to imagine that shallow copies are faster than deep copies, but in the sense of copying, shallow copies are a bit less than deep copies.
Examples are as follows:
#include using namespace std;//20200430 public number: C language and CPP programming
class CopyDemo{public: CopyDemo(int pa,char *cstr) //constructor, two arguments { this->a = pa; this->str = new char[1024]; //pointer array, dynamically allocate storage space on heap with new strcpy(this->str,cstr); //copy over}
//not written, C++ will automatically help write a copy constructor, shallow copy only copy pointers, the following comments part//CopyDemo(CopyDemo& obj) //{ // this->a = obj.a; // this->str = obj.str; //here is a shallow copy will cause problems, to copy deeply//}
CopyDemo(CopyDemo& obj) //general data members have pointers to write copy constructor function, as follows { this->a = obj.a; // this->str = obj.str; //here is a shallow copy will cause problems, to copy deeply this->str = new char[1024];//should be written like this if(str != 0) strcpy(this->str,obj.str); //if successful, copy the contents}
~CopyDemo() //destructor { delete str; }
public: int a; //defines an integer data member char *str; //string pointer};
int main(){ CopyDemo A(100,"hello!!! ");
CopyDemo B = A; //copy the constructor, put A's 10 and hello!!! Copy to B cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.