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
7
  • This array is a member of a class? A short example would do wonders to help people understand. Jun 1, 2018 at 21:31
  • Compiles just fine for me - you need to post more context.
    – user2100815
    Jun 1, 2018 at 21:31
  • @NeilButterworth How does it compile for you? ideone.com/HVx1dQ Jun 1, 2018 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. Jun 1, 2018 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. Jun 1, 2018 at 21:40

3 Answers 3

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" };
};
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"};
2
  • C++17 supports inline variables - a feature introduced specifically to combat the excessive repetitiveness of this approach. Jun 1, 2018 at 22:12
  • 1
    It should be const char *, otherwise it doesn't compile. Jun 1, 2018 at 22:22
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.