zoukankan      html  css  js  c++  java
  • 【c++】必须在类初始化列表中初始化的几种情况

    本文转载自 http://www.cnblogs.com/kaituorensheng/p/3477630.html,感谢作者分享

    1. 类成员为const类型

    2. 类成员为引用类型

    复制代码
    #include <iostream>
    using namespace std;
    
    class A
    {
        public:
            A(int &v) : i(v), p(v), j(v) {}
            void print_val() { cout << "hello:" << i << "  " << j << endl;}
        private:
            const int i;
            int p;
            int &j;
    };
    
    int main(int argc ,char **argv)
    {
        int pp = 45;
        A b(pp);
        b.print_val();
    }
    复制代码

    究其因

    const对象或引用只能初始化但是不能赋值。构造函数的函数体内只能做赋值而不是初始化,因此初始化const对象或引用的唯一机会是构造函数函数体之前的初始化列表中。

    从无到有叫初始化,初始化(调用拷贝构造函数)创建了新对象;赋值(调用赋值操作符)没有创建新对象,而是对已有的对象赋值。

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class Base
     5 {
     6     public:
     7     Base(){cout << "Base()" << endl;}
     8         Base(int a) : val(a) {cout << "Base(int a) : val(a)" << endl;}
     9     private:
    10         int val;
    11 };
    12 
    13 class A
    14 {
    15     public:
    16         A(int v) : p(v),b(v) {cout << "A(int v) : p(v), b(v)" << endl;}
    17         void print_val() { cout << "hello:" << p << endl;}
    18     private:
    19         int p;
    20         Base b;
    21 };
    22 
    23 int main(int argc ,char **argv)
    24 {
    25     int pp = 45;
    26     A b(pp);
    27     b.print_val();
    28 }
    View Code

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class Base
     5 {
     6     public:
     7     Base(){cout << "Base()" << endl;}
     8         Base(int a) : val(a) {cout << "Base(int a) : val(a)" << endl;}
     9     private:
    10         int val;
    11 };
    12 
    13 class A : public Base
    14 {
    15     public:
    16         A(int v) : p(v) {cout << "A(int v) : p(v), Base(v)" << endl;}
    17         void print_val() { cout << "hello:" << p << endl;}
    18     private:
    19         int p;
    20 };
    21 
    22 int main(int argc ,char **argv)
    23 {
    24     int pp = 45;
    25     A b(pp);
    26     b.print_val();
    27 }
    View Code

    #include <iostream>
    using namespace std;
    
    class Base
    {
        public:
        //Base(){cout << "Base()" << endl;}
            Base(int a) : val(a) {cout << "Base(int a) : val(a)" << endl;}
        private:
            int val;
    };
    
    class A : public Base
    {
        public:
            A(int v) : p(v),Base(v) {cout << "A(int v) : p(v), Base(v)" << endl;}
            void print_val() { cout << "hello:" << p << endl;}
        private:
            int p;
    };
    
    int main(int argc ,char **argv)
    {
        int pp = 45;
        A b(pp);
        b.print_val();
    }
    View Code

    3. 类成员为没有默认构造函数的类类型

    复制代码
    #include <iostream>
    using namespace std;
    
    class Base
    {
        public:
            Base(int a) : val(a) {}
        private:
            int val;
    };
    
    class A
    {
        public:
            A(int v) : p(v), b(v) {}//因为Base有自定义的带参构造函数,就不会产生无参构造函数,就无法产生类对象了
            void print_val() { cout << "hello:" << p << endl;}
        private:
            int p;
    Base b; }; int main(int argc ,char **argv) { int pp = 45; A b(pp); b.print_val(); }
    复制代码

    原因同样是创建对象时,要初始类成员的每一个成员

    4. 如果类存在继承关系,派生类必须在其初始化列表中调用基类的构造函数

    复制代码
    #include <iostream>
    using namespace std;
    
    class Base
    {
        public:
            Base(int a) : val(a) {}
        private:
            int val;
    };
    
    class A : public Base
    {
        public:
            A(int v) : p(v), Base(v) {}
            void print_val() { cout << "hello:" << p << endl;}
        private:
            int p;
    };
    
    int main(int argc ,char **argv)
    {
        int pp = 45;
        A b(pp);
        b.print_val();
    }
    复制代码
  • 相关阅读:
    解决中文环境下zabbix监控图形注释乱码
    SSIS CDC(Change Data Capture)组件在数据库中启用报错。 The error returned was 14234: 'The specified '@server' is invalid
    Tableau 群集部署
    访问Tableau自带的PostgreSQL数据库
    [译]Stairway to Integration Services Level 18 – 部署和执行
    [译]Stairway to Integration Services Level 16 – Flexible Source Locations (多文件导入)
    [译]Stairway to Integration Services Level 15 – SSIS 参数回顾
    [译]Stairway to Integration Services Level 14
    [译]Stairway to Integration Services Level 13
    [译]Stairway to Integration Services Level 12
  • 原文地址:https://www.cnblogs.com/guxuanqing/p/4910229.html
Copyright © 2011-2022 走看看