I mentioned the copy constructor in my previous blog. However, it is hard to understand the copy constructor in some cases. So let's discuss the copy constructor.
The copy constructor is dividedShortest copyAndDeep copy: the default copy constructor is a light copy.
By default, the copy constructor copies the memory space of the old object to the memory space of the new object.
If the class has a pointer type, the default copy constructor can only copy the value of the pointer property, but cannot copy the memory pointed to by the pointer property.At this time, if we do not explicitly define the copy constructor, we may encounter very strange problems during programming.
Explicitly define the copy constructor to copy pointer properties and other properties that require special processing.
The Number one: Let's first look at The problems caused by The shallow copy.
--------------------- I am a split line ------------------------
- # Include <iostream>
- Using namespace std;
- Class Car
- {
- Private:
- Char * brand;
- Float price;
- Public:
- Car (const char * sz, float p)
- {
- // The constructor allocates memory for the brand.
- Brand = new char [strlen (sz) + 1];
- Strcpy (brand, sz );
- }
- ~ Car
- {
- // Release the applied memory in the destructor
- Delete [] brand;
- Cout <"Clear is over! "<Endl;
- }
- Void just_print ()
- {
- Cout <"brand:" <brand <endl;
- Cout <"price:" <price <endl;
- }
- };
- Int main (void)
- {
- Car_one ("BMW", 120 );
- Car_one.just_print ();
- // Call the default copy constructor
- Car car_two (comp_one );
- Car_two.print ();
- Return 0;
- }
----------------------------------------------------------------------------
This program failed. Code Analysis:
1. car_two (car_one) is equivalent
Car_two.brand = car_one.brand;
Car_two.price = car_one.price;
2. After the assignment operation, the pointers in the two objects point to the same dynamic memory. When car_one and car_two are revoked, the function to release the same dynamic memory must be released. However, after two objects are undo first, once one object is undo, the brand pointer of the other object changes to "Wild Pointer". Using this pointer to release the same dynamic memory will cause a memory error.
Not only is the memory released repeatedly, but other problems may occur:
-------------------------------------------------------------------------------
- Int main (void)
- {
- Car car_one ("Dell", 7000 );
- If (true)
- {
- Car car_two (car_one );
- Car_two.print ();
- }
- // The dynamic memory pointed to by car_one.brand has been released.
- Car_one.print ();
- Return 0;
- }
Bytes -------------------------------------------------------------------------------------------
Because car_two is a local object defined in the if structure, when the if structure exits, car_two is revoked and the system automatically calls its destructor to release the dynamic memory pointed to by car_two.brand, because the car_one and car_two values are the same, car_one.brand has no fingers and becomes a wild pointer. In this case, read/write operations on the pointer will cause unexpected errors.
----------------------------------------------------------------------------
In this case, we need to define the copy constructor by ourselves:
----------------------------------------------------------------------------
- // Explicitly define the constructor
- # Include <iostream>
- # Include <cstring>
- Using namespace std;
- Class Car
- {
- Private:
- Char * brand;
- Float price;
- Public:
- Car (const char * sz, float p)
- {
- Brand = new char [strlen (sz) + 1];
- Strcpy (brand, sz );
- Price = p;
- }
- // Custom copy constructor
- Car (const Car & cp)
- {
- // Re-create a memory space of the same size as cp. brand for brand
- Brand = new char [strlen (cp. brand) + 1];
- //
- Strcpy (brand, cp. brand );
- Price = cp. price;
- }
- ~ Car ()
- {
- Delete [] brand;
- Cout <"clear over" <endl;
- }
- Void print ()
- {
- Cout <"brand" <endl;
- Cout <"price" <endl;
- }
- };
- Int main (void)
- {
- Car car_one ("Dell", 8999 );
- Car_one.print ();
- //
- Car car_two (car_one );
- Car_two.print ();
- // The user does not directly assign values using brand = cp. brand, but re-applies for dynamic memory.
- // The database function strcpy copies strings.
- // Car_one.brand and car_two.brand point to two different memories to avoid errors.
- Return 0;
- }
Bytes ------------------------------------------------------------------------------------------
Finally, we recommend that you reload the operator = operator for a custom copy constructor!
Bytes -----------------------------------------------------------------------------------------
51cto blog: http://liam2199.blog.51cto.com/2879872/1417892