C++ 期末考了这道题:
我惊讶地发现,int a(0), b(0);
是错误的。
我们知道,自 C++11 起已经支持非静态成员变量的类内初始化了。
而对基本数据类型的初始化形式 int a(0);
也是没有争议的,那么为什么类内初始化是不被允许的?
我找到了这篇文章:Non-static data member initializers(非静态数据成员初始化器)
大概说的是一些语义不清晰的问题。
有时候我们无法避免下列情况。
Problem 1:
Unfortunately, this makes initializers of the “( expression-list )” form ambiguous at the time that the declaration is being parsed:
struct S {
int i(x); // data member with initializer
// ...
static int x;
};
struct T {
int i(x); // member function declaration
// ...
typedef int x;
};