zoukankan      html  css  js  c++  java
  • 构造函数初始化列表

    通过构造函数初始化参数,可能经常会这么干:

    class TEST
    {
    private:
        /* data */
    public:
        TEST(int abc,int bcd,int fds);
        ~TEST();
        
    public:
        int abc;
        int bcd;
        int fds;
    };
    
    TEST::TEST(int abc,int bcd,int fds)
    {
        this->abc = abc;
        this->bcd = bcd;
        this->fds = fds;
    }
    
    TEST::~TEST()
    {
    }

    为了简化这种写法,我们采用初始化列表

    class TEST
    {
    private:
        /* data */
    public:
        TEST(int a,int b,int c);
        ~TEST();
    
    
    public:
        int abc;
        int bcd;
        int fds;
    };
    
    TEST::TEST(int a,int b,int c):abc(a),bcd(b),fds(c)
    {
        
    }
    
    TEST::~TEST()
    {
    }

    和上面的写法作用相同,都是把局部变量的值传给test的成员变量

    也可以不写参数,直接给他赋值

    class TEST
    {
    private:
        /* data */
    public:
        TEST();
        ~TEST();
    
    
    public:
        int abc;
        int bcd;
        int fds;
    };
    
    TEST::TEST():abc(5),bcd(6),fds(7)
    {
        
    }
    
    TEST::~TEST()
    {
    }

    这样在使用TEST的时候他们的值 直接就是 5 6 7

    测试:

    #include <iostream>
    #include <stdio.h>
    
    
    using namespace std;
    
    class TEST
    {
    private:
        /* data */
    public:
        TEST();
        ~TEST();
    
        void p_printf();
    
    public:
        int abc;
        int bcd;
        int fds;
    };
    
    TEST::TEST():abc(5),bcd(6),fds(7)
    {
        
    }
    
    TEST::~TEST()
    {
    }
    
    void TEST::p_printf()
    {
        printf("---%d---
    ",this->abc);
        printf("---%d---
    ",this->bcd);
        printf("---%d---
    ",this->fds);
    }
    
    int main(int argc, char const *argv[])
    {
        TEST test;
        test.p_printf();
        return 0;
    }

    输出:

    ---5---
    ---6---
    ---7---

    初始化 const 成员变量的唯一方法就是使用初始化列表

    假设上面例子中的成员都是const修饰的

    class TEST
    {
    private:
        /* data */
    public:
        TEST(int a,int b,int c);
        ~TEST();
    
        void p_printf();
    
    public:
        const int abc;
        const int fds;
        const int bcd;
    };
    
    TEST::TEST(int a,int b,int c)
    {
        this->abc = a;
        this->bcd = b;
        this->fds = c;
        
    }
    
    TEST::~TEST()
    {
    }

    这样初始化是不会成功的

    报错如下

    test.cpp: In constructor ‘TEST::TEST(int, int, int)’:
    test.cpp:23:1: error: uninitialized const member inconst int’ [-fpermissive]
     TEST::TEST(int a,int b,int c)
     ^
    test.cpp:18:12: note: ‘const int TEST::abc’ should be initialized
      const int abc;
                ^
    test.cpp:23:1: error: uninitialized const member inconst int’ [-fpermissive]
     TEST::TEST(int a,int b,int c)
     ^
    test.cpp:19:12: note: ‘const int TEST::fds’ should be initialized
      const int fds;
                ^
    test.cpp:23:1: error: uninitialized const member inconst int’ [-fpermissive]
     TEST::TEST(int a,int b,int c)
     ^
    test.cpp:20:12: note: ‘const int TEST::bcd’ should be initialized
      const int bcd;
                ^
    test.cpp:25:12: error: assignment of read-only member ‘TEST::abc’
      this->abc = a;
                ^
    test.cpp:26:12: error: assignment of read-only member ‘TEST::bcd’
      this->bcd = b;
                ^
    test.cpp:27:12: error: assignment of read-only member ‘TEST::fds’
      this->fds = c;
                ^

    这个时候只能选择用初始化例表的方式初始化const修饰的成员变量

    class TEST
    {
    private:
        /* data */
    public:
        TEST(int a,int b,int c);
        ~TEST();
    
        void p_printf();
    
    public:
        const int abc;
        const int fds;
        const int bcd;
    };
    
    TEST::TEST(int a,int b,int c):abc(a),bcd(b),fds(c)
    {    
    }
    
    TEST::~TEST()
    {
    }
  • 相关阅读:
    C#获取配置文件中的文件数据
    wpf MVVMLight的DataGrid绑定数据
    扫码支付自动跳转,可以使用第三方网站实现扫码二维码付款然后跳转到想要的页面展示想要内容或者是解压码或者是某个资源的下载页呢 具体步骤(我以你上传一个压缩包到某种网盘或者可以下载的地址等让人付费解压为例):
    oracle数据库如何创建用户以及分配权限
    ORA-12547: TNS: 丢失连接
    springmvc中applicationapplicationContext头部代码
    No mapping found for HTTP request with URI
    在Navicat新建用户
    myeclipse 项目引入 com.sun.image.codec.jpeg 的api报错解决方法
    java.lang.NullPointerException at org.apache.jsp.**_jsp.jspInit(**_jsp.java)tomcat启动异常解决方法
  • 原文地址:https://www.cnblogs.com/qifeng1024/p/13596234.html
Copyright © 2011-2022 走看看