构造函数初始化列表

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

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---\n",this->abc);
    printf("---%d---\n",this->bcd);
    printf("---%d---\n",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()
{
}

 

posted @ 2020-09-01 15:01  祁峰_1024  阅读(146)  评论(0编辑  收藏  举报