1

I tried to initialize an array of string in class as following:

class Haab{
    string str[];
    Haab(){
        str[] = {"abc", "abd", "abe"};
    }
};

But the Devc++ 5.6.1 reports a warning:

[Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]

Is this way of initializing arrays in class illegal? If so, how to properly initialize the array? Thank you.

Zining Zhu
  • 313
  • 1
  • 2
  • 12
  • As the warning says, its a new feature from C++11 (see: [here](http://en.wikipedia.org/wiki/C%2B%2B11#Initializer_lists)). C++03, the previous standard, only allowed initializer-list syntax to be used with POD data types (which string is not). – cartographer Dec 07 '14 at 03:49
  • I changed "string" to "char" but it reported the same warning. By the way what is initializer-list syntax? – Zining Zhu Dec 07 '14 at 03:51
  • Looking at your code, again, is that the actual code you are trying to compile? `str[] = {"abc", "abd", "abe"};` isn't even valid syntax as far as I know (separate from the declaration that is). – cartographer Dec 07 '14 at 03:55
  • Yes I tried to compile that code. Then what is the correct syntax to do initialize such array? – Zining Zhu Dec 07 '14 at 04:08
  • As an addendum to Alf's answer, if you do syntax like `char str[]` in a struct, its probably a [flexible array member](http://stackoverflow.com/questions/4412749/are-flexible-array-members-valid-in-c?lq=1) (not valid in C++) – cartographer Dec 07 '14 at 04:22

1 Answers1

2

The given code,

class Haab{
    string str[];
    Haab(){
        str[] = {"abc", "abd", "abe"};
    }
};

is invalid in a number of ways:

  • string str[]; declares a member array of unknown size. Can't do that.

  • In str[] = {"abc", "abd", "abe"};, the expression str[] uses the [] indexing operator without specifying the index.

  • If that parsed, then the = would denote assignment of a single string.

Here's one C++03 way to do things:

#include <string>
#include <vector>

namespace qwe {
    using std::string;
    using std::vector;

    class Haab
    {
    private:
        vector<string> str_;

    public:
        Haab()
        {
            static char const* const data[] = {"abc", "abd", "abe"};

            for( int i = 0; i < 3; ++i )
            {
                str_.push_back( data[i] );
            }
        }
    };
}  // namespace qwe

There are also other C++03 ways, all of them ugly (as the above).

In C++11 and later it can be done with more elegant & simple notation;

#include <string>
#include <vector>

namespace qwe {
    using std::string;
    using std::vector;

    class Haab
    {
    private:
        vector<string> str_;

    public:
        Haab()
            : str_{ "abc", "abd", "abe"}
        {}
    };
}  // namespace qwe

but this does still not compile with Visual C++ 13.0 (it does compile with MinGW g++ 4.9.1).

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Thanks. No warnings from compiler now. There's still a question though. In line16 you wrote 'static char const* const data[]='. What does "const* const" mean? (I also tried const char* data[] and it works. Are the two expressions equivalent?) – Zining Zhu Dec 07 '14 at 04:30
  • @ZiningZhu: If you had not omitted the second `const` they would have been equivalent. I use the syntax `T const` because that's the *general* syntax, e.g. it's what you have to use for the second `const`. But for the basic type of the declaration you can write `const T` for readability, and many prefer that even though it doesn't generalize; in particular it's done in all the standard's examples. – Cheers and hth. - Alf Dec 07 '14 at 04:40