zoukankan      html  css  js  c++  java
  • 构造函数调用规则

    首先我们知道只要创建一个类编译器会提供三个默认函数

    1.默认构造函数 (无参,空实现) 

    2.默认析构函数(无参,空实现)

    3.默认拷贝构造函数,对值属性进行拷贝

    调用规则如下

    1.如果我们定义有参构造函数,编译器不会提供默认构造函数,但提供默认拷贝构造函数

    2.如果用户定义了拷贝构造函数,编译器将不会提供其他构造函数

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 class person
     5 {
     6 public:
     7     person()
     8     {
     9         puts("默认构造函数调用");
    10     }
    11 
    12     person(const person &p)
    13     {
    14         m_age = p.m_age; 
    15         puts("拷贝构造函数调用");
    16     }
    17 
    18     person(int age)
    19     {
    20         m_age = age;
    21         puts("有参构造函数调用");
    22     }
    23 
    24     ~person()
    25     {
    26         puts("析构函数调用");
    27     }
    28 
    29     int m_age;
    30 };
    31 
    32 void test()
    33 {
    34     person p;
    35     p.m_age = 18;
    36     person p2(p);
    37     cout << p2.m_age << endl; 
    38 }
    39 
    40 int main()
    41 {
    42     test();
    43     return 0;
    44 }
    View Code

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 class person
     5 {
     6 public:
     7     person()
     8     {
     9         puts("默认构造函数调用");
    10     }
    11 
    12 //    person(const person &p)
    13 //    {
    14 //        m_age = p.m_age; 默认拷贝构造函数会执行这里,把所有值属性拷贝 
    15 //        puts("拷贝构造函数调用");
    16 //    }
    17 
    18     person(int age)
    19     {
    20         m_age = age;
    21         puts("有参构造函数调用");
    22     }
    23 
    24     ~person()
    25     {
    26         puts("析构函数调用");
    27     }
    28 
    29     int m_age;
    30 };
    31 
    32 void test()
    33 {
    34     person p;
    35     p.m_age = 18;
    36     person p2(p);
    37     cout << p2.m_age << endl; 
    38 }
    39 
    40 int main()
    41 {
    42     test();
    43     return 0;
    44 }
    View Code

    下面代码证明了定义有参构造函数,编译器不会提供默认构造函数,但提供默认拷贝构造函数

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 class person
     5 {
     6 public:
     7     person()
     8     {
     9         puts("默认构造函数调用");
    10     }
    11 
    12 //    person(const person &p)
    13 //    {
    14 //        m_age = p.m_age; 默认拷贝构造函数会执行这里,把所有值属性拷贝 
    15 //        puts("拷贝构造函数调用");
    16 //    }
    17 
    18     person(int age)
    19     {
    20         m_age = age;
    21         puts("有参构造函数调用");
    22     }
    23 
    24     ~person()
    25     {
    26         puts("析构函数调用");
    27     }
    28 
    29     int m_age;
    30 };
    31 
    32 void test()
    33 {
    34     person p;
    35     p.m_age = 18;
    36     person p2(p);
    37     cout << p2.m_age << endl; 
    38 }
    39 
    40 void test01()
    41 {
    42     person p(22);
    43     person p2(p);
    44     cout << p2.m_age << endl;
    45  } 
    46 int main()
    47 {
    48     //test();
    49     test01();
    50     return 0;
    51 }
    View Code

    关于第二个规则,如果你可以把除了拷贝构造函数的其他构造函数去掉,你运行是报错的

    说明如果用户定义了拷贝构造函数,编译器将不会提供其他构造函数

  • 相关阅读:
    cookie和session
    ViewState与Session 的重要区别
    C#.Net 中ArrayList 与 Array的区别
    c#如何把文件夹压缩打包然后下载
    关于并发的处理
    const和readonly的区别
    计算某列的字符串相加sql语句
    MS SQL Server存储过程批量修改用户表所有者
    已有打开的与此命令相关联的DataReader,必须首先将它关闭
    XML与数据库
  • 原文地址:https://www.cnblogs.com/mch5201314/p/11602407.html
Copyright © 2011-2022 走看看