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

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

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

  • 相关阅读:
    Android APK反编译
    android 安卓APP获取手机设备信息和手机号码的代码示例
    Android-- ArrayAdapter用法举例(转载)
    Android--ListView 分割线
    Android——检测TXT文件中是否含有双字节字符
    Android--------从一个包中的Avtivity创建另外另外一个包的Context
    百度地图技术大揭秘
    Lotusscript统计在线用户数
    代理中如何获取参数么
    DXL之通过程序修改Domino的设计
  • 原文地址:https://www.cnblogs.com/mch5201314/p/11602407.html
Copyright © 2011-2022 走看看