Describes in detail the copy constructor of C ++ class objects.

Source: Internet
Author: User

Copy constructorIs a special constructor called by the compiler to construct and initialize Other Objects Based on the same class. Its unique parameter object reference) is an unchangeable const type ). This function is often used to pass and return user-defined type values during function calls. The copy constructor calls the copy constructor and member functions of the base class. If possible, it will be called in constant mode, and can also be called in a very large way.

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

 
 
  1. int a=100;  
  2. int b=a; 

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.

 
 
  1. #include <iostream>  
  2. using namespace std;  
  3. class CA  
  4. {  
  5. public:  
  6. CA(int b)  
  7. {  
  8. a=b;  
  9. }  
  10. void Show ()  
  11. {  
  12. cout<<a<<endl;  
  13. }  
  14. private:  
  15. int a;  
  16. };  
  17. int main()  
  18. {  
  19. CA A(100);  
  20. CA B=A;  
  21. B.Show ();  
  22. 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.

 
 
  1. #include <iostream>  
  2. using namespace std;  
  3. class CA  
  4. {  
  5. public:  
  6. CA(int b)  
  7. {  
  8. a=b;  
  9. }  
  10. CA(const CA& C)  
  11. {  
  12. a=C.a;  
  13. }  
  14. void Show()  
  15. {  
  16. cout<<a<<endl;  
  17. }  
  18. private:  
  19. int a;  
  20. };  
  21. int main()  
  22. {  
  23. CA A(100);  
  24. CA B=A;  
  25. B.Show ();  
  26. 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, for example, 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.

 
 
  1. # Include <iostream>
  2. Using namespace std;
  3. Class CA
  4. {
  5. Public:
  6. CA (int B, char * cstr)
  7. {
  8. A = B;
  9. Str = new char [B];
  10. Strcpy (str, cstr );
  11. }
  12. CA (const CA & C)
  13. {
  14. A = C.;
  15. Str = new char [a]; // deep copy
  16. If (str! = 0)
  17. Strcpy (str, C. str );
  18. }
  19. Void Show ()
  20. {
  21. Cout <str <endl;
  22. }
  23. ~ CA ()
  24. {
  25. Delete str;
  26. }
  27. Private:
  28. Int;
  29. Char * str;
  30. };
  31. Int main ()
  32. {
  33. Ca a (10, "Hello! ");
  34. Ca B =;
  35. B. Show ();
  36. Return 0;
  37. }

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

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.