1

Strangely there's almost no online documentation on this subject. I have an application with hard coded strings for the UI and various other things scattered through the source. I'm attempting to round up at least some of them because a number of them need to change depending on what platform the application is compiled for.

I used the second example here (copied below for reference) but it's somewhat light on the details of how this should actually work. It appears that everything is reinitialized in project_strings.cpp and the code is never actually called.

// in your project_strings.h
namespace MyProjectStrings {
const char *password;
...
}

// the project_strings.cpp for the strings
#include "project_strings.h"
namespace MyProjectStrings {
const char *password = "Password:";
...
}

// some random user who needs that string
#include "project_strings.h"
std::string password(MyProjectStrings::password);

Can someone explain this or tell me its a terrible idea and I should do something else?

Community
  • 1
  • 1
pdel
  • 685
  • 12
  • 23
  • Isn't this explained in the question/answer from where you copied it? Also I do not really understand how your question is different from the one where you copied this...maybe its a duplicate – 463035818_is_not_an_ai Apr 22 '15 at 14:30
  • The answer just posts the code. I'm asking how exactly its supposed to work. The variables all look like they're re-declared in project_strings.cpp. Also, it seems that none of the code in project_strings.cpp is ever actually run since every time I call MyProjectStrings::password I get nothing. – pdel Apr 22 '15 at 14:37
  • Still what you are asking isn't different from the original question and I think you should have commented on the answer (to ask for clarification) instead of asking a new question. I might be wrong, but to me it smells like a duplicate – 463035818_is_not_an_ai Apr 22 '15 at 14:39
  • The question asked is Qt specific. I'm asking about C++ standard practices. – pdel Apr 22 '15 at 14:55
  • try with 'c' extern. That is you only declare the variable in one location. Then you can define the variable with 'extern' in each place , where you gonna use it. (don't forget to declare the variable - may lead to crashes) – ANjaNA Apr 22 '15 at 15:49

1 Answers1

0

The example you link is simply declaring some const char*s in a namespace in a header file and then defining them in a .cpp file. The syntax used is incorrect for C++ though - the declarations in the header file should be extern const char* rather than const char* as const values default to internal linkage in C++.

Most C++ compiler/linker toolchains will place all the strings from the one translation unit (project_strings.cpp) together in a read only data segment of the resulting executable.

This is a reasonable, simple approach to handling static strings although you probably want something a bit more dynamic / sophisticated if you need to handle localization or other complexities.

mattnewport
  • 13,728
  • 2
  • 35
  • 39
  • Understood, but why is the value of all the strings (null) when I attempt to access them in files that include "project_strings.h"? – pdel Apr 22 '15 at 15:09
  • 1
    Actually, the example code in the linked answer is wrong, you need an explicit `extern` in the header file declarations since `const` values are not implicitly `extern`. – mattnewport Apr 22 '15 at 15:39