1

I am creating a console program in C++.

I have static text lines like "error wrong usage, usage is: program <parameter> <parameter2>" or "game over!"

I recently had a C project where all code had to be all in one c file. I just made lots of defines like this:

#define ERRUSAGE "Usage error: correct usage is ./bla - bla2 -etc"

after the includes and before the function predefinitions.

Now I'm working on a c++ program where it's allowed to have multiple files. I've learned that I would use a .h file to define something and a .cpp file to implement something.

So is the way to go here making a staticTextOutput.h and fill it up with

#ifndef BLABLA
#define BLABLA "sometext"
#endif

and then include it where I need it?

What's the best practise here?

o/

EDIT:

Thanks for reminding me about making a const instead of using defines; that's a big point.

To refine my question: I'd also like to know what the file structure should look like (since .h files traditionally contain declarations and .cpp files definitions).

I know how this should look for functions and classes where would a struct (like the one Xaqq posted as answer) go since it has static/const members?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
t0b4cc0
  • 319
  • 4
  • 19

4 Answers4

9

A common way to deal with ingame strings (dialogs, etc) is to have them stored in an asset file that you load once at runtime and store in variables.

This way you avoid recompiling everything to fix a typo, you can easily work with multiple languages, etc.

cmourglia
  • 2,423
  • 1
  • 17
  • 33
  • whats the best practice approach here In what filetype should I put it? in the .h or the .cpp file? (If I use a struct like Xaqq used in his Answer) Whats a reason to use a struct instead of just put all of them in a namespace? – t0b4cc0 Mar 23 '16 at 19:26
  • I'm not sure about your question.You can have your string in a txt file and parse it at runtime. – cmourglia Mar 24 '16 at 16:01
1

Since this is tagged C++, I'd encourage to stay away from macros when possible.

If supported by your compiler, I would make use of constexpr to define those static text.

struct Messages
{
 constexpr static const char * const blabla = "sometext";
};

You can then use it like this:

std::cout << Messages::blabla << std::endl;
Xaqq
  • 4,308
  • 2
  • 25
  • 38
1

I use character arrays:

static const char my_text[] = "Three little froggies, ready to jump.";

The static const allows the compiler to place the text in a read-only section and because there's only one instance, the compiler can access it directly (without copying to the stack).

The array notation allows for the length to be calculated at compile-time:

const unsigned int text_length = sizeof(my_text) - 1U;

The 1U represents the nul termination character.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

I would write this at the top of document below include's:

const static std::string CErrorMessage = "Usage error: correct usage is ./bla - bla2 -etc";
Vinigas
  • 464
  • 5
  • 18
  • Why all the heavy weight of `std::string` when simple `static const char* const` would do the trick? – SergeyA Mar 23 '16 at 16:33
  • In C++ I prefer `std::string`s. In C I prefer `char*`. – Vinigas Mar 23 '16 at 16:35
  • 1
    @Vinigas Use `std::string` when you really need it. – πάντα ῥεῖ Mar 23 '16 at 16:36
  • @Vinigas, why? To prefer X over Y, X should have benefits over Y, which outweight costs of X over Y! This is the engineering approach. What are the benefits of `std::string` in this case? Are you aware of it's costs? – SergeyA Mar 23 '16 at 16:37
  • Firstly, `string` are more comfortible to use and manipulate. They have modifiers, e.g., `insert`, `erase`. Also in my opinion you should always use `std::string` in C++ OOP because `std::string` is OO itself. If you have strings in your program and string constant, then you don't waste time converting char* to std::string. – Vinigas Mar 23 '16 at 16:42
  • @Vinigas, again - in this scenario how do you expect to use modifiers? You are having a `const std::string` yourself! Second, C++ does not equate with OOP. And there is a good argument that `std::string` does not actually follow OO path. – SergeyA Mar 23 '16 at 16:46
  • Ok, but what about converting these `char*` all the time (`to_string()`) then you need it in other program places. Using `string` only can make code more clearier. And also with `char*` only, you can't know length. Length knowing is usefull if you want to fit text correctly, for example. It's my opinion. – Vinigas Mar 23 '16 at 17:02
  • @Vinigas, it's much better to have the constant length known at compile time of your literal, rather than calculate it every time in run-time. As for 'when you need string', if your function wants a const std::string it is better to accept `const char*`. If it needs non-const string, your string literal won't do anyways. – SergeyA Mar 23 '16 at 17:20