In modern C++, member variables can be explicitly initialized. However, while we can omit the capacity of an array when defining a local array in a function with an initializer list, we cannot omit the capacity of an array member even if it has an initializer list. For example, the following definition,
class A{
int a[]{1,2,3,4}; // error
int b[4]{};
int c[4]{1,2,3,4};
};
gives "array bound cannot be deduced from an in-class initializer" error because of member variable a. I'm curious why this kind of definition is not allowed.
Aucun commentaire:
Enregistrer un commentaire