Rather simple question.
Where should I store error,exception, user messages?
By far, I always declared local strings inside the function where it is going to be invoked and did not bother.
e.g.
SomeClass::function1(...)
{
std::string str1("message1");
std::string str2("message2");
std::string str3("message3");
...
// some code
...
}
Suddenly I realized that since construction & initialization are called each time and it might be quite expensive. Would it be better to store them as static strings in class or even in a separate module?
Localization is not the case here.
Thanks in advance.
Why not just use a string constant when you need it?
Alternately, you can use
static const std::strings. This is appropriate if you expect to copy them to a lot of otherstd::strings, and your C++ implementation does copy-on-write:If you expect to make lots of copies of these
strings, and you don’t have copy-on-write (or can’t rely on it being present), you might want to look into using boost::flyweight.