C++: constructor initializer for arrays
There is no way. You need a default constructor for array members and it will be called, afterwards, you can do any initialization you want in the constructor.
There is no way. You need a default constructor for array members and it will be called, afterwards, you can do any initialization you want in the constructor.
To get the equivalent of a static constructor, you need to write a separate ordinary class to hold the static data and then make a static instance of that ordinary class. class StaticStuff { std::vector<char> letters_; public: StaticStuff() { for (char c=”a”; c <= ‘z’; c++) letters_.push_back(c); } // provide some way to get at … Read more
No, an empty initializer list is not allowed. This can also be shown by GCC when compiling with -std=c99 -pedantic: a.c:4: warning: ISO C forbids empty initializer braces The reason is the way the grammar is defined in ยง6.7.9 of the 2011 ISO C Standard: initializer: assignment-expression { initializer-list } { initializer-list , } initializer-list: … Read more
This question was the subject of my blog on September 20th 2010. Josh and Chad’s answers (“they add no value so why require them?” and “to eliminate redundancy”) are basically correct. To flesh that out a bit more: The feature of allowing you to elide the argument list as part of the “larger feature” of … Read more
I am not 100% positive about this, but this might be to prevent a syntax ambiguity. For example, consider the following class: class BadTimes { struct Overloaded; int Overloaded; // Legal, but a very strange idea. int confusing(Overloaded); // <– This line }; What does the indicated line mean? As written, this is a declaration … Read more