为什么C++的创建者决定使用构造函数初始化列表来初始化基类?

Kei*_*awa 3 c++

为什么C++的创建者决定使用构造函数初始化列表来初始化基类?为什么他没有选择使用类似下面代码中的第二个注释行的语法?

class A{
public:
  A() { }
};

class B : A{
public:
  B() : A() { }   // Why decide to use this one, using constructor initializer, to initialize base class?
  B() { A(); }    // Why not choose this one? It's easy for me to understand if it was possible.
};

int main(int argc, char *argv[]){
  /* do nothing */
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*eis 5

使用初始化列表的优点是,您在构造函数的主体中有一个完全初始化的对象.

class A {
public:
    A(const int val) : val(val) { }
private:
    const int val;
};

class B : A{
public:
  B() : A(123) {
      // here A and B are already initialized
  }
};
Run Code Online (Sandbox Code Playgroud)

因此,您可以保证所有成员,甚至是父成员,都不处于未定义状态.

编辑

在问题的示例中,没有必要在初始化列表中调用基类,这是自动完成的.所以我略微修改了这个例子.