Several details about copying constructors in C ++

Source: Internet
Author: User

Article by: http://grantren.iteye.com/blog/43289

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 <iostream> </iostream>
  2. # Include <string> </string>
  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. Class X {
  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. Class X {
  2. Public:
  3. X ();
  4. X (X &);
  5. };
  6. Const x 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. Struct X {
  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)

 

4.Shortest copy and deep copy

In some cases, the member variables in the class need to dynamically open up the heap memory. If a bit (SHAP) copy is implemented, the values in the object are completely copied to another object, for example, a = B. If a member variable pointer in B has applied for memory, the member variable in a also points to the same memory. This causes a problem: when B releases the memory (such as the destructor), the pointer in A is a wild pointer and a running error occurs.

Deep copy and shallow copy can be simply understood as: If a class has resources, when the objects of this class are copied, the resources are re-allocated. This process is a deep copy, and vice versa, if no resources are re-allocated, it is a small copy. The following is an example of deep copy.

# Include <iostream> using namespace STD; Class ca {public: Ca (int B, char * CSTR) {A = B; STR = new char [B]; strcpy (STR, CSTR);} Ca (const ca & C) {A = C. a; STR = new char [a]; // If (STR! = 0) strcpy (STR, C. Str);} void show () {cout <STR <Endl ;}~ CA () {Delete STR;} PRIVATE: int A; char * STR ;};
int main(){ CA A(10,"Hello!"); CA B=A; B.Show(); return 0;} 

The definition of deep copy and shallow copy can be simply understood as: If a class has resources (heap, or other system resources), when the object of this class is copied, this process can be called Deep copy. Otherwise, the object has resources, but the copying process does not copy resources.

A program running error occurs when resources are released after a shallow copy.

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.