带分配的成员声明与 ctors 有何关系?

how does member declaration with assignment relate to ctors?

抱歉 - 我确定我找不到任何答案,因为我不知道术语!

class Foo
{
public:
  Foo() { }   // default ctor
  explicit Foo(int a) : _a(a) { }   // semi-explicit - but what is _b's value??
protected:
  int _a = 9;  // HOW DOES THIS RELATE TO EACH OF THE CTORS?!
  int _b = 3;  // HOW DOES THIS RELATE TO EACH OF THE CTORS?!
};

通过显式指定默认构造函数 w/o 指定 _a 或 _b,声明的赋值是否发生(_a = 9,_b = 3)或者仅在我 不发生时才发生 创建默认构造函数(或者我声明为 Foo() = default;)?

C++11 [class.base.init]/8:

In a non-delegating constructor, if a given non-static data member or base class is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer) and the entity is not a virtual base class of an abstract class (10.4), then

— if the entity is a non-static data member that has a brace-or-equal-initializer, the entity is initialized as specified in 8.5;

— ...

因此,在您的情况下,默认初始化的 Foo 具有 _a == 9_b == 3,因为 _a_b 都没有出现在(不存在) mem-initializer-listFoo(7) 将有 _a == 7_b == 3,因为 _b 没有出现在 mem-initializer-list.