0

EDIT: please note, as stated in @ThomasMatthews answer, it is better not to put data in a header. Please refer to his answer.

I would like to create a static const char* const array in the header file of a class. For example: const static char* ar[3] = {"asdf","qwer","ghjk"}; However I get an error.

Here is an example:

#include <iostream>
class test{
  static const char* const ar[3] = {"asdf","qwer","hjkl"};
}
int main(){}

and here is the error:

static data member of type 'const char *const [3] must be initialized out of line

I'd like to know if what I am trying to do is possible. I have read Defining static const integer members in class definition and the impression I got from it was that you can only do this with int. In case this is relevant I am using a mac and my g++ version is as follows:

Apple LLVM version 9.1.0 (clang-902.0.39.1)
Target: x86_64-apple-darwin17.5.0
Thread model: posix
Mathew
  • 1,116
  • 5
  • 27
  • 59
  • This array is a member of a class? A short example would do wonders to help people understand. – Retired Ninja Jun 01 '18 at 21:31
  • Compiles just fine for me - you need to post more context. –  Jun 01 '18 at 21:31
  • @NeilButterworth How does it compile for you? https://ideone.com/HVx1dQ – Killzone Kid Jun 01 '18 at 21:34
  • I recommend against placing data in a header file. Each source file that includes the header gets a copy of the data. You may want to make an `extern` declaration for the data and place the data in a source file. – Thomas Matthews Jun 01 '18 at 21:37
  • `caused me to wonder if there is more to this` There isn't. Once you `const` the pointer as per @melpomene answer you will get another error, this time what you expect. – Killzone Kid Jun 01 '18 at 21:40
  • @Killzone This `const static char* ar[3] = {"asdf","qwer","ghjk"};` compiles for me with g++ 7.2.0. –  Jun 01 '18 at 21:41
  • @ThomasMatthews thanks, you have a good point, if you give an example of that I'll accept that as an answer – Mathew Jun 01 '18 at 21:45

3 Answers3

5

The need to provide a separate out-of-line definition (with an initializer) for static class members is rooted in the fact that the exact point of definition in C++ affects the order of initialization and location of the exported symbol in object files. The language wants you to make these decisions yourself.

However, to simplify things in those frequent cases when you don't care about such stuff, starting from C++17 you can do exactly what you want by specifying an explicit inline keyword in static member declaration

class test{
  static inline const char* const ar[] = { "asdf", "qwer", "hjkl" };
};
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
2

I recommend not placing data into a header file.
Any source file including the header will get a copy of the data.

Instead, use an extern declaration:

data.h:

extern char const * data[3];

data.cpp:

char const * data[3] = {"asdf","qwer","hjkl"};
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

This worked even if I put all of it into the header file (in which case however, line_001 has to be after the class definition block, just as shown below). But you can also put the line_001 into the cpp file.

With the const pointer:

class MyClass
{
public:
    MyClass();
    static char const* const arr[3];
};



char const* const MyClass::arr[3] = {"aaaa", "bbbbb", "ccccc"};

Without the const pointer:

class MyClass
{
public:
    static const char* arr[3];
};

const char* MyClass::arr[3] = {"aaaa", "bbbbb", "ccccc"}; // line_001