0

I am trying to find a way that I can define/initialize some static string arrays in a struct inside a class definition. These string arrays are always to be initialized with string literals. I tried doing the following and it initializes the parameterTypes[] fine and i can access the strings using eg. MethodNaming_bla::parameterTypes[2], but if I pass the parameterTypes as a const char* const * const i get a linker error :( Also if i pass it to a constructor using MethodNaming_bla::parameterTypes.

struct MethodNaming_bla
{
    static constexpr const char* const parameterTypes[] = { "afd","qwer","zvc" };
    static const char* const * const parameterTypes_d = parameterTypes;
};

However if i do the same in a function, it works for some reason.

int main(int, char* [])
{
    static constexpr const char* const parameterTypes[] = { "afd","qwer","zvc" };
    static const char* const * const parameterTypes_d = parameterTypes;
}

Is there a way where I can define and initialize a string array in a struct from string literals, inline?

Thanks in advance, Martin

Martin Vang
  • 371
  • 3
  • 12
  • You could use an Initializer_list of strings, and define them after the class definition. – Ben Apr 14 '14 at 07:50

1 Answers1

0

Ok, thanks for your comments. Kerrek, the dublicate u mention explain why it doesnt work, but it does not solve my problem for me, because I need the initialization to be inline - initalizing it in the implementation file is therefore not an option for me.

Guess my only option is something like:

struct MethodNaming_bla
{
    static const char* const * const ParameterTypes()
    {
        static const char* const parameterTypes[] = { "afd","qwer","zvc" };
        return parameterTypes;
    };
};
Martin Vang
  • 371
  • 3
  • 12