Copy constructor analysis of c ++ class objects

Source: Internet
Author: User

For common objects, copying between them is very simple, for example:

Int A = 100;
Int B =;
Different from common objects, class objects have complicated internal structures and various member variables. The following is a simple example of copying a class object.

# Include <iostream>
Using namespace STD;
Class ca
{
Public:
CA (int B)
{
A = B;
}
Void show ()
{
Cout <A <Endl;
}
PRIVATE:
Int;
};

Int main ()
{
Ca a (100 );
Ca B =;
B. Show ();
Return 0;
}

Run the program and output 100 on the screen. From the running results of the above code, we can see that the system allocates memory for object B and completes the replication process with object. For class objects, class objects of the same type are copied to the constructor to complete the entire replication process. The following is an example of how to copy a constructor.

# Include <iostream>
Using namespace STD;
Class ca
{
Public:
CA (int B)
{
A = B;
}
CA (const ca & C)
{
A = C.;
}
Void show ()
{
Cout <A <Endl;
}
PRIVATE:
Int;
};

Int main ()
{
Ca a (100 );
Ca B =;
B. Show ();
Return 0;
}

CA (const ca & C) is our custom copy constructor. It can be seen that a copy constructor is a special constructor. The name of a function must be the same as the class name. Its unique parameter is a reference variable of this type, this parameter is of the const type and is unchangeable. For example, the copy constructor of Class X is in the form of X (X & X ).

When an initialized custom class object is used to initialize another newly constructed object, the copy constructor is automatically called. That is to say, when the class object needs to be copied, the copy constructor will be called. The copy constructor is called in the following cases:

An object passes in the function body as a value.

An object is returned from the function by passing values.

An object needs to be initialized through another object.

If a copy constructor is not explicitly declared in the class, the compiler automatically generates a default copy constructor, which completes the bitwise copy between objects. Bit copy, also known as the shortest copy, will be described later.

Custom copy constructor is a good programming style. It can prevent the compiler from forming a default copy constructor and improve the source code efficiency.
Shortest copy and deep copy

In some cases, the member variables in the class need to dynamically open up the heap memory. If bit copy is implemented, the values in the object are completely copied to another object, such as a = B. If a member variable pointer in B has applied for memory, the member variable in a also points to the same memory. This causes a problem: when B releases the memory (such as the destructor), the pointer in A is a wild pointer and a running error occurs.

Deep copy and shallow copy can be simply understood as: If a class has resources, when the objects of this class are copied, the resources are re-allocated. This process is a deep copy, and vice versa, if no resources are re-allocated, it is a small copy. The following is an example of deep copy.

# Include <iostream>
Using namespace STD;
Class ca
{
Public:
CA (int B, char * CSTR)
{
A = B;
STR = new char [B];
Strcpy (STR, CSTR );
}
CA (const ca & C)
{
A = C.;
STR = new char [a]; // deep copy
If (STR! = 0)
Strcpy (STR, C. Str );
}
Void show ()
{
Cout <STR <Endl;
}
~ CA ()
{
Delete STR;
}
PRIVATE:
Int;
Char * STR;
};

Int main ()
{
Ca a (10, "Hello! ");
Ca B =;
B. Show ();
Return 0;
}

Let's talk about this. I hope this article will help you.

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.