C ++ copy constructor

Source: Internet
Author: User

Author: Qing Dujun


1) Definition:A copy constructor is a special constructor called by the compiler to construct and initialize Other Objects Based on the same class. The only parameter must be referenced, but is not limited to const. Generally, the const limit is added. 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.


2) Call the copy constructor:
In C ++, the following three objects need to call the copy constructor (sometimes called the copy constructor "):
A) An object acts as a function parameter and passes in the function body as a value;
B) An object acts as the return value of the function and is returned from the function by passing the value;
C) an object is used to initialize another object (often called value assignment initialization );

If you do not use the copy constructor In the first two cases, a pointer is directed to the deleted memory space. In the third case, the difference between initialization and assignment is the reason for calling the copy constructor. In fact, the copy constructor is implemented by the common constructor and the value assignment operator. There are many references describing the similarities and differences between the copy constructor and the value assignment operator.
The general principle is: ① A copy constructor should be provided for classes that contain dynamically allocated members or pointer members; ② while providing a copy constructor, you should also consider overloading the "=" value assignment operator. For the reason, see the following document.
The copy constructor must be passed as a reference (the parameter is the reference value ). The reason is as follows: when an object transmits a function by passing a value, the copy constructor is automatically called to generate the object in the function. If an object is passed in to its own copy constructor, its copy constructor will be called to copy this object so that it can pass in its own copy constructor, this will lead to an infinite loop until Stack Overflow ). In addition to being implicitly called when an object is passed into a function, the copy constructor is also called when the object is returned by the function.

(The text above comes from the copy constructor of Baidu encyclopedia)


3) What you need to mention is explicit:

C ++ provides the keyword explicit to prevent implicit conversions that should not be allowed by conversion constructors. Constructors declared as explicit cannot be used in implicit conversions.

# Include
 
  
Using namespace std; class A {public: A (int a) {m_a = a;} // explicit: the constructor cannot forcibly convert A single number to private: int m_a ;}; int main () {a A (3); A B = 6; // equivalent to A B = A (6), Force convert 6 to return 0 ;}
 

Note: Only A single number can be A B = 6. However, this is not recommended. Follow the (3) method above.


Example: C ++ copy constructor

# Include
 
  
Using namespace std; class Cpy {public: Cpy (int nA = 0, double douB = 0.0); // constructor Cpy (Cpy & p); // copy the constructor private: int m_nA; double m_douB;}; // constructor Cpy: Cpy (int nA, double douB) {m_nA = nA; m_douB = douB; cout <"Tip: Cpy:: Cpy (int nA, double douB )... "<
  
   
Running result:
   


References: Baidu encyclopedia, copy constructor, http://baike.baidu.com/view/1266959.htm, May 24, 2014

Baidu encyclopedia, explicit, http://baike.baidu.com/view/2422253.htm, August

Xiong Si's CSDN blog, C ++ constructor, http://blog.csdn.net/u010056#/article/details/26623069, May 24, 2014

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.