C++ 的intialization list 和assignment

[补充]

如果一个类的所有成员都是public的,而且没有默认构造函数,就可以使用intialization list对类的成员进行初始化。

三大法

#include <iostream>
#include <map>
using namespace std;

class A
{
public:
    A(int a):i(a),j(a){
        //i = a;   // i is not modifiable here
    }
    const int i;
    int & j;
};

class B:public A
{
public:
    B():A(0)
    {
    }

    B(int a):A(a) // B's base class A only has one constructor A(int a), so you must provide a way to initilize A's parameters;
    {
    }
}
原文地址:https://www.cnblogs.com/whyandinside/p/2689315.html