PROBLEM:
1. Which function is a copy constructor, why? X :: x (const X); x :: x); x :: x (x &, int a = 1); x :: x (x &, int A = 1, b = 2); 2. Can there be more copy construct functions in a class?
3. Write the output result of the following block and explain why? #Include
Solution:
Copy Constructor Definition: For a class x, if a constructor is one of the following: a) X & B) Const X & C) Volatile X & D) The default value without other parameters or other parameters, Then this function is a copy constructor.
EG: x :: x (const x "; // is a copy constructor x :: x (x &, int = 1); // is a copy constructor can exist more than one copy constructor, Eg: class x { Public: x (const X &); x (x &); // OK Note that there is only one parameter to the X & copy constructor, then CONST X or VOLATILE X object cannot be used to implement copy initialization. eg: Class x {public: x (); x (x&);}; const x cx; // error If a copy constructor is not defined in a class, the compiler will automatically generate a default copy constructor This default parameter may be x :: x (const X &) or x :: x (x ", which is determined by the compiler according to the context.
The default copy constructor behavior is as follows: The default copy constructor execution is the same as the other user-defined constructor, performs the construction of the prior parent class subclass. Copy constructor execute member copy of each data member in the class (MEMBERWISE COPY) Action. A) If the data member is an instance of a certain class, then call this type of copy constructor. B) If the data member is an array, each of the groups of the array performs a bit copy. C) If the data member It is a quantity, such as int, double, then invoking the system built-in assignment assignment.
The copy constructor cannot be generated by the template. EG: struct x {template
Template