C ++ copy constructor details

Source: Internet
Author: User
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?

  1. X: X (const X &);
  2. X: x (x );
  3. X: X (X &, int A = 1 );
  4. 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.

  1. # Include
  2. # Include
  3. Struct X {
  4. Template <typename T>
  5. X (T &) {STD: cout <"this is ctor." <STD: Endl ;}
  6. Template <typename T>
  7. X & operator = (T &) {STD: cout <"this is ctor." <STD: Endl ;}
  8. };
  9. Void main (){
  10. X a (5 );
  11. X B (10.5 );
  12. X c =;
  13. C = B;
  14. }

 

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.

  1. X: X (const X &); // copy the constructor.
  2. X: X (X &, Int = 1); // copy the constructor.

2. There can be more than one copy constructor in the class,

  1. ClassX {
  2. Public:
  3. X (const X &);
  4. X (X &); // OK
  5. };

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.

  1. ClassX {
  2. Public:
  3. X ();
  4. X (X &);
  5. };
  6. ConstX cx;
  7. 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.

  1. StructX {
  2. Template <typename T>
  3. X (const T &); // not copy ctor, t can't be X
  4. Template <typename T>
  5. Operator = (const T &); // not copy ass't, t can't be X
  6. };

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)

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.