• When does compiler create default and copy constructors in C++?


      In C++, compiler creates a default constructor if we don’t define our own constructor (See this). Compiler created default constructor has empty body, i.e., it doesn’t assign default values to data members (In java, default constructors assign default values).

      Compiler also creates a copy constructor if we don’t write our own copy constructor. Unlike default constructor, body of compiler created copy constructor is not empty, it copies all data members of passed object to the object which is being created.

      What happens when we write only a copy constructor – does compiler create default constructor?
      Compiler doesn’t create a default constructor if we write any constructor even if it is copy constructor.

      For example, the following program doesn’t compile.

     1 #include <iostream>
     2 using namespace std;
     3  
     4 class Point
     5 {
     6     int x, y;
     7 public:
     8    Point(const Point &p) { x = p.x; y = p.y; }
     9 };
    10  
    11 int main()
    12 {
    13     Point p1;  // COMPILER ERROR
    14     Point p2 = p1;
    15     return 0;
    16 }

      Output:  COMPILER ERROR: no matching function for call to 'Point::Point()
      

      What about reverse – what happens when we write a normal constructor and don’t write a copy constructor?
      Reverse is not true. Compiler creates a copy constructor if we don’t write our own. Compiler creates it even if we have written other constructors in class. For example, the below program works fine.

     1 #include <iostream>
     2 using namespace std;
     3  
     4 class Point
     5 {
     6     int x, y;
     7 public:
     8    Point(int i, int j) 
     9    { 
    10           x = 10; 
    11           y = 20; 
    12    }
    13    int getX() 
    14   { 
    15           return x; 
    16    }
    17    int getY() 
    18   { 
    19           return y; 
    20    }
    21 };
    22  
    23 int main()
    24 {
    25     Point p1(10, 20);
    26     Point p2 = p1; // This compiles fine
    27     cout << "x = " << p2.getX() << " y = " << p2.getY();
    28     return 0;
    29 }

      Output:  x = 10 y = 20
      

      So we need to write copy constructor only when we have pointers or run time allocation of resource like file handle, a network connection, etc

      下面的内容摘自《深入探索C++对象模型》。

      对于class X,如果没有任何user-declared constructor,那么会有一个default constructor被暗中(implicitly)声明出来.....一个被暗中声明出来的default constructor将是一个trivial(浅薄而无能,没哈用的)constructor......

      一个nontrivial default constructor就是编译器需要的那种,必要的话会由编译器合成出来。下面分别讨论nontrivial default constructor的四种情况。

      (1)“带有Default Constructor”的Member Class Object

      如果一个class没有任何constructor,但它内含一个member object,而后者有default constructor,那么这个class的implicit default constructor就是“nontrivial”,编译器需要为此class合成出一个default constructor。不过这个合成操作只有在constructor 真正需要被调用时才会发生。

      (2)“带有Default Constructor”的Base Class

      类似的道理,如果一个没有任何constructors的class派生自一个“带有default constructor”的base class,那么这个derived class的default constructor会被视为nontrivial,并因此需要合成出来。它将调用上一层base classes的default constructor(根据它们的声明次序)。对一个后继派生的class而言,这个合成的constructor和一个“被明确提供的default constructor”没有什么差异。

      (3)“带有一个Virtual Function”的Class

      另有两种情况,也需要合成出default constructor:

      <1> class声明(或继承)一个virtual function;

      <2> class派生自一个继承串链,其中有一个或更多的virtual base class

      不管哪一种情况,由于缺乏由user声明的constructors,编译器会详细记录合成一个default constructor的必要信息。

      (4)“带有一个Virtual Base Class”的Class

      

      C++新手一般有两个常见的误解:

      (1)任何class如果没有定义default constructor,就会被合成出一个来。

      (2)编译器合成出来的default constructor会明确设定“class内每一个data member的默认值”。

      如你所见,没有一个是真的。

      Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
      

      转载请注明:http://www.cnblogs.com/iloveyouforever/

      2013-11-26  19:36:09

  • 相关阅读:
    asp.net 验证正则表达式
    c语言编程题
    使用Code First创建数据模型
    c语言知识点
    rabbitmq 简单应用
    influxdb(二)
    influxdb(一)
    K8S 日志收集(六):kibana 安装
    K8S 日志收集(五):head插件安装
    K8S 日志收集(四):logstash 安装
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3443969.html
Copyright © 2020-2023  润新知