Introduction to a simple example of copying Class C 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 is passed in the function body as a value.
* An object is returned from a 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;
}

Related Article

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.