What is the correct way in C++ to create a global & static table of strings?
By "global", I mean: Useable from any file that includes the header. But not part of some run-time created singelton objcet.
By "static", I mean: As little run time set up possable. Data in read only memory pages. Only 1 instance of data per app.
By "string", I mean: Null terminated array of chars is fine. std::string would be nice, but I don't think it can be done in terms of the above. Correct?
By "table", I mean: I mean an indexable array. So I guess not a table per-se. But I'm flexable on this point. Open to ideas.
By "C++", I mean: C++ not C. (Update: C++98, not C++11)
Use a
std::array
of string literals. It has no constructor so it will be loaded statically in the.rodata
section like a C array, yet it has a standard C++ library interface. (iterators, size, etc)A.h:
A.cpp:
http://en.cppreference.com/w/cpp/container/array
The
table_n
provided strictly to ensure you at least have a hint how big it is:Table.h
Table.cpp
Or something like that.
strings.h
strings.cpp
Another take on this, using error codes for a lookup table:
err.h
err.cpp
main.cpp
I'm sure there is a more C++ way of doing this, but I'm really a C programmer.
I like Jonathon Reinhart way, I always do it that way especially if we have a structure of elements,
however, it requires a loop to find the elements (Not indexed), so if you like an improvement, especially for embedded systems style.