Named namespace in implementation file for grouping string literals const - Good / Bad?

I grouped several lines of messages into a named (non-anonymous) namespace in a .cpp file to output class processing, as shown in the code below:

namespace Messages
{
  static const std::string AppTitle = "The Widgetizer - Serving all your Widget needs";
  static const std::string SuccessMsg = "Great success! Widgets for all! ";
  static const std::string FailMsg = "No widgets for you!";
};

void Display::printTitle()
{
  out << Messages::AppTitle << std::endl;
}

void Display::printSuccessMsg()
{
  out << Messages::SuccessMsg << std::endl;
}

void Display::printFailMsg()
{
  out << Messages::FailMsg << std::endl;
}

My logic is that in this way they are all in one central place, under the namespace with a meaningful and self-documenting name, and they are not exposed to the client code (as it would be if I put the namespace in the .h file) .

Is this good practice at all or are there pitfalls that I don't see?

Is a static keyword necessary if they are in the namespace for a file area like this?

++, , ? const?

, , , , , , , , , , - ?

+3
4

, ?

, ; , , .

, , ?

static, const, , . static , const , , - extern .

++, , ? const?

, ; - . , , .

0

, , . " ", . , , -. ++, . .

+1

​​ , ?

, .

, (, cpp ) . , , .

, , ?

.

++, , ? const?

. , .

, CPP .

, , , , , , , , , , - ?

DRY, , , , , . , , , .

0

, .

:

  • ( ).
  • .
  • , .

, , , . 3000+ . .

1 2 3 / .

0

Source: https://habr.com/ru/post/1759192/


All Articles