A copy constructor is a member function defined for copying between class members. For many classes, the composite copy constructor only performs the necessary work. Classes that only contain Class Members or built-in type members (but not pointer type members) can be copied without explicitly defining the copy constructor. When should the replication constructor be customized. When a pointer is defined in the class, resources are dynamically allocated, or some specific operations must be completed in the class, a custom copy constructor is required. My friend told me that when deep copy is involved, we need to explicitly define a copy constructor, such as memory operations and pointers. In addition, you need to implement specific functions during the copy process, and you also need to explicitly define the constructor !, For example, you need to calculate the number of specific objects of a class.
I don't quite understand the specific deep copy, but I have such a concept in my mind. Some friends said they would like to have an in-depth understanding of the object model. If you want to design classes related to data structures, such as data storage, You need to customize them.
Pointer problem: if we use the composite copy constructor to copy the pointer, it will have terrible consequences. For example: String STR = "ooo"; string str2 = "AAA"; When STR = str2 is used, the following effect is displayed: First, the memory that STR once pointed to will never be deleted and will be lost forever. This is a typical example of Memory leakage. Second, the pointers in STR and str2 point to the same string, so long as one of them leaves its living space, the Destructor will delete the memory that another pointer points. At this time, the space indicated by STR has changed and the memory cannot be released. If str2 is released, this memory will be stored by the system for other values, and cannot be accessed when STR is accessed again.
This is the replication constructor I understand. I hope you will make more suggestions.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service