我想使用初始化列表来初始化一个结构体,就像这样:
struct S
{
int a;
int b;
// S() : a(0), b(0){} // uncommenting will cause compile error:
// error C2440: 'initializing' : cannot convert from 'initializer-list' to 'S'
// S(int aArg, int bArg) : a(aArg), b(bArg) {} // adding this removes the error
}
int main()
{
S s{1,2}; // initialise with list
}
为什么明确定义的默认构造函数会导致错误?我以为初始化列表是为了避免程序员编写冗长的代码,如第二个构造函数。