In depth summary of the C++ copy constructor

  • 2020-04-01 23:35:52
  • OfStack

Copy constructor is one of the most basic concepts in C++. Please answer three questions first:
1. Which of the following functions is a copy constructor, and 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 of the following program segment and explain why. If you can answer all of these correctly, you already know a lot about copy constructors.
 
# include < iostream >  
# include < The string >        
Struct {X    
  The template < Typename T >    
  X(T&) {STD ::cout < < "This is ctor." < < STD: : endl; }    

  The template < Typename T >    
      X& operator=(T&) {STD ::cout < < "This is ctor." < < STD: : endl; }    
};    

Void main () {    
  X a (5);    
  X b (10.5);    
  X c = a;    
  C = b;    
}  

The answer is as follows:
1. For a class X, if the first argument to a constructor is one of the following:
      A) X &
      B) const & X
      C) volatile & X
      D) const volatile & X
      This function is a copy constructor if no other parameters or all other parameters have default values.
      X: : X (const & X);   // is the copy constructor    
      X: : X (X &, int = 1); // is the copy constructor  
      X: : X (X &, int a = 1, b = 2).   // is the copy constructor  

2. There can be more than one copy constructor in a class,
The class X {          
Public:          
  X (const X &);          
  X (X &);                       / / OK    
};  
Note that you cannot use const X or volatile X objects for copy initialization if there is only one copy constructor with an argument of X& in a class.
The class X {    
Public:    
  (X);    
  X (X &);    
};    

Const X cx.    
X X = cx.       / / the error    
If a copy constructor is not defined in a class, the compiler automatically produces a default copy constructor. The default parameter may be X::X(const X&) or X::X(X&), depending on the context.
The default Copy constructor behaves as follows: the default Copy constructor executes in the same order as the other user-defined constructors, performing the construction of the parent and subsequent subclasses.
  A) if the data member is an instance of a class, the copy constructor of that class is called.
  B) if the data member is an array, perform a bitwise copy of each array.
  C) if the data member is a number, such as int,double, then the system's built-in assignment operator is called to assign it.

3.   A copy constructor cannot be generated by a member function template.
Struct {X    
      The template < Typename T >    
      X (const & T);       // NOT copy ctor, T can't be X    

      The template < Typename T >    
      Operator = (const & T);   // NOT copy ass don 't, t can't be X    
};    

The reason is very simple, a member function template does not alter the rules of the language, the language rule says that if a program requires a copy constructor and you did not declare it, then the compiler will automatically generate a for you. So a member function template does not prevent the compiler to generate the copy constructor, assignment operator overloading also follow the same rules


Related articles: