C ++ constructor/destructor/copy constructor/deep copy and shallow copy Parsing

Source: Internet
Author: User
Document directory
  • Shortest copy
  • Deep copy

References: Call Sequence of constructor and destructor in C ++

1. participate in real parameters

Parameter: it is a parameter used in function declaration. It only indicates the parameter name and type. It is not an actual parameter and cannot be used.
Real parameter: The parameter passed to the function at run time is the actual variable. The actual space is allocated and the value of the real parameter is copied.

A functionReal parameters have their own fixed memory in the memoryThe memory will not be released until the function execution ends. WhileThe parameter does not have a fixed memory., There is only one virtual memory when the function is called, and no memory will be available after the call is completed .. Their relationship is that when a function is called, the real parameters pass the value to the form parameter.

2. Constructor
  1. The constructor cannot return values. The function name is a class name.
  2. When the default constructor is used, the system automatically calls the default constructor to initialize the object. The default constructor initializes all data members to zero or null. The default constructor does not contain parameters.
  3. When an object is created, the system automatically calls the constructor.
3. destructor

The Destructor has no parameters or return values. It cannot be overloaded. That is to say, only one destructor can be defined in a class. If no destructor is defined in a class, the system automatically generates a default destructor, which is an empty function and does nothing. Call conditions:

  1. An object defined in the function body. When the function execution ends, the destructor of the class of the object will be automatically called;(Define an object in a function. When the function call ends, the Destructor is automatically called to delete the object created in the function. Including the main function .)
  2. An object dynamically built using the new operator is released using the delete operator.
4. copy constructors

A copy constructor is actually a constructor. It has all the features of a General constructor, and its name is the same as its class name. The copy constructor has only one parameter, which is a reference to a similar object.
Called in three cases: see http://blog.csdn.net/bluescorpio/article/details/4322682

  1. When initializing another object of the class with a known object of the class.("A = B" can be used for initialization, or a (B .)
  2. The parameter of a function is a Class Object. When you call a function to combine the parameter with the real parameter.(Define a function A. The parameters of function a are class objects. In another function B, call this function A. The real parameters pass specific objects to the form parameters, at this time, the copy constructor will be called .)
  3. The return value of a function is a Class Object. After the function is executed, the caller is returned.(Define a function a. the return value of this function is a Class Object. Define the Class Object in function B to accept the return value of function A. At this time, the copy constructor is called .)
5. Deep copy and light copy

The so-called shallow copy refers to simply assigning values to the data members in the object during object replication. The above examples are all in the case of a shallow copy,By default, the copy constructor executes the shortest copy function.. In most cases, "Shallow copy" can work well, but once an object existsDynamic MemberIn this case, a problem occurs when copying a copy. Let's consider the following code:

View code

Class rect
{
Public:
Rect () // constructor. P points to a space allocated in the heap.
{
P = new int (100 );
}
~ Rect () // destructor to release the dynamically allocated space
{
If (P! = NULL)
{
Delete P;
}
}
PRIVATE:
Int width;
Int height;
Int * P; // a pointer member
};

Int main ()
{
Rect rect1;
Rect rect2 (rect1); // copy an object
Return 0;
}

A running error occurs before the code is executed. The reason is that during object replication, the dynamically allocated content is not correctly operated. Let's analyze:

After running the definition rect1 object, since there is a dynamically allocated statement in the constructor, the memory after execution is roughly as follows:

When rect1 is used to copy rect2, The rect1.p and rect2.p have the same value as the rect2, because the rect1 is used to copy the rect2, only the value of the member is assigned, that is, the two pointers point to the same space in the heap, as shown in:

Of course, this is not the expected result,When the object is destroyed, the destructor of the two objects will be released twice for the same memory space. This is why the error occurs.. What we need is not that two P have the same value, but two PPointThe space has the same value. The solution is to use "Deep copy ".

Deep copy

In the case of "Deep copy", for dynamic members of an object, it is not just a simple assignment, but a dynamic allocation of space should be performed., The above example should be processed as follows:

View code

Class rect
{
Public:
Rect () // constructor. P points to a space allocated in the heap.
{
P = new int (100 );
}
Rect (const rect & R)
{
Width = R. width;
Height = R. height;
P = new int; // dynamically allocate space for the new object
* P = * (R. P );
}
~ Rect () // destructor to release the dynamically allocated space
{
If (P! = NULL)
{
Delete P;
}
}
PRIVATE:
Int width;
Int height;
Int * P; // a pointer member
};

After copying an object, the memory usage is roughly as follows:

In this case, the p of rect1 and the P of rect2 point to a memory space, but they point to the same content, which is called "Deep copy ".

Summary:

The copy constructor can be divided into deep copy and shortest copy. Generally, the shortest copy satisfies the requirements. However, when there are dynamic members, shortest copy cannot meet the requirements. For example, if an object has pointer members, you can only point the Copied object to the same region through a simple shortest copy, rather than creating an area of the same size. This requires deep copy.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.