• 类和对象(7):初始化列表


    class Date
    {
    public:
    	Date(int year = 1, int month = 1, int day = 1)
    	{
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    构造函数体内的语句只能称为赋初值,不能称为初始化。初始化只能初始化一次,而构造函数体内可以多次赋值。

    一、初始化列表

    1.1 定义

    初始化列表:以一个:开始,用,分隔的数据成员列表,每个“成员变量”后跟一个(),其中放初始值或表达式

    // Date类
    Date(int year = 1, int month = 1, int day = 1)
        :_year(year)
        ,_month(month)
        ,_day(day)
    {}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    // Stack类
    class Stack()
    {
    public:
        Stack(int capacity = 3)
            :_a((int*)malloc(sizeof(int) * capacity))
            ,_top(0)
            ,_capacity(capacity)
        {
        	if (nullptr == _a) 
            {
                perror("malloc");
    		}
        }
    private:
        int* _a;
        int _top;
        int _capacity;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    PS:

    1. 每个成员变量只能在初始化列表出现一次。(只能初始化一次

    2. 类中包含引用成员变量const成员变量自定义类型成员(且该类没有默认构造函数时),必须在初始化列表进行初始化

    class A
    {
    public:
    	A(int a)
    		:_a(a)
    	{}
    private:
    	int _a;
    };
    
    class B
    {
    public:
    	B(int d = 1)
    		:_a1(1)// 没有默认构造函数
    		,_b(d)// 引用成员变量
    		,_b2(2)// const成员
    	{}
    private:
    	A _a1;
    	int& _b;
    	const int _b2;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    除此以外,初始化列表和函数体可以混用,如上Stack类中情形。

    1. 成员变量在类中的声明次序就是成员变量在初始化列表中初始化顺序,与其在初始化列表中出现先后无关。

    观察以下情形:

    class A
    {
    public:
    	A(int a)
    		:_a1(a)
    		,_a2(_a1)
    	{}
    private:
    	int _a2;
    	int _a1;
    };
    
    int main()
    {
    	A a(1);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

  • 相关阅读:
    4.物联网射频识别,RFID开发【智能门禁项目】
    【web-解析目标】(1.1.3)解析内容和功能:发现隐藏的内容、参数
    Vue、jquery和angular之间区别
    哈夫曼树(理论)
    Python基础语法
    C++命名空间详解
    makefile(详细讲解)
    软件测试需求分析
    Keepalived 高可用详解
    让人疑惑的STM32F4F7芯片
  • 原文地址:https://blog.csdn.net/taduanlangan/article/details/134427753