C ++ copy constructor details
Keywords: C ++
Copy constructor is one of the most basic concepts of C ++. Do you think you know about copy constructor? Please answer the following three questions:
1.Which of the following functions is a copy constructor? Why?
- X: X (const X &);
- X: x (x );
- X: X (X &, int A = 1 );
- X: X (X &, int A = 1, B = 2 );
2.Can there be more than one copy constructor in a class?
3.Write the output results of the following sections and explain why? If the answer is correct, you have a good understanding of the copy constructor.
- # Include
- # Include
- Struct X {
- Template <typename T>
- X (T &) {STD: cout <"this is ctor." <STD: Endl ;}
- Template <typename T>
- X & operator = (T &) {STD: cout <"this is ctor." <STD: Endl ;}
- };
- Void main (){
- X a (5 );
- X B (10.5 );
- X c =;
- C = B;
- }
The answer is as follows:
1.For a class X, if the first parameter of a constructor is one of the following:
A) X &
B) const X &
C) Volatile X &
D) const volatile X &
If no other parameters or other parameters have default values, this function is a copy constructor.
- X: X (const X &); // copy the constructor.
- X: X (X &, Int = 1); // copy the constructor.
2. There can be more than one copy constructor in the class,
- ClassX {
- Public:
- X (const X &);
- X (X &); // OK
- };
NOTE: If only one copy constructor with the parameter X & exists in a class, the object of const X or volatile x cannot be used for copy initialization.
- ClassX {
- Public:
- X ();
- X (X &);
- };
- ConstX cx;
- X = Cx; // Error
If a copy constructor is not defined in a class, the compiler automatically generates a default copy constructor.
This default parameter may be X: X (const X &) or X: X (X &), which is determined by the compiler based on the context.
The default copy constructor behavior is as follows:
The execution sequence of the default copy constructor is the same as that of other user-defined constructor.
The copy constructor performs the member copy (memberwise copy) action on each data member in the class.
A) if the data member is an instance of a class, the copy constructor of this class is called.
B) if the data member is an array, perform a bitwise copy on each of the arrays.
C) if the number of data members is an integer, such as Int or double, assign values to them by calling the built-in value assignment operator.
3.The copy constructor cannot be generated by the member function template.
- StructX {
- Template <typename T>
- X (const T &); // not copy ctor, t can't be X
- Template <typename T>
- Operator = (const T &); // not copy ass't, t can't be X
- };
The reason is very simple. The member function template does not change the language rules. The language rules say that if the program requires a copy constructor and you do not declare it, the compiler will automatically generate one for you. therefore, the member function template does not prevent the compiler from generating a copy constructor. The reload of the value assignment operator follows the same rule. (See Objective C ++ 3 edition, item45)