初始化列表操作实际上就是构造函数在初始化成员时的一个语法糖。
    例如传统方法初始化:

    1. #include <iostream>
    2. #include <string>
    3. using namespace std;
    4. class Person {
    5. public:
    6. Person(int a, int b, int c) {
    7. m_a = a;
    8. m_b = b;
    9. m_c = c;
    10. }
    11. int m_a;
    12. int m_b;
    13. int m_c;
    14. };
    15. void Example() {
    16. Person p1(10, 20, 30);
    17. cout << "m_a = " << p1.m_a << endl;
    18. cout << "m_b = " << p1.m_b << endl;
    19. cout << "m_c = " << p1.m_c << endl;
    20. }
    21. int main()
    22. {
    23. Example();
    24. return 0;
    25. }

    列表化:简化后的

    1. #include <iostream>
    2. #include <string>
    3. using namespace std;
    4. class Person {
    5. public:
    6. Person() : m_a(10), m_b(20), m_c(30) {}
    7. int m_a;
    8. int m_b;
    9. int m_c;
    10. };
    11. void Example() {
    12. Person p1;
    13. cout << "m_a = " << p1.m_a << endl;
    14. cout << "m_b = " << p1.m_b << endl;
    15. cout << "m_c = " << p1.m_c << endl;
    16. }
    17. int main()
    18. {
    19. Example();
    20. return 0;
    21. }

    或者赋值的形式可以写成

    1. #include <iostream>
    2. #include <string>
    3. using namespace std;
    4. class Person {
    5. public:
    6. Person(int a,int b,int c) : m_a(a), m_b(b), m_c(c) {}
    7. int m_a;
    8. int m_b;
    9. int m_c;
    10. };
    11. void Example() {
    12. Person p1(10,20,30);
    13. cout << "m_a = " << p1.m_a << endl;
    14. cout << "m_b = " << p1.m_b << endl;
    15. cout << "m_c = " << p1.m_c << endl;
    16. }
    17. int main()
    18. {
    19. Example();
    20. return 0;
    21. }