How do I default initialize a member array in C++11? It seems that I have to provide a bound.
class Example {
const char* num2letter[10]{" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
};
Compiles fine. But if I omit the bound then I got a compiler error:
error: too many initializers for 'const char* [0]' const char* num2letter[]{" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
How come the compiler cannot infer the bound as normally it can for local array definition using aggregate initialization?
A default member initializer is just that - the default. It always allows for the possibility that a constructor will provide a different value. num2letter
could potentially be initialized from one of Example
's constructors, at which point the default initializer wouldn't even be used... so the type has to be explicitly provided. The declaration has to be valid with our without the initializer.
This is the same reason that you can't do something like:
class Example {
auto i = 4;
};