I have string constants, for strings that I use in multiple places in my app:
namespace Common{
static const std::string mystring = "IamAwesum";
}
When posting a question about something else (What happens to a .h file that is not included in a target during compilation?), another user made the following comment :
be aware that your static string are global in this case. So they are could create an exception at anytime and can't be catch. I advise you to use function who return a reference of your string. std::string const &mystring { static std::string const mystring = "IamAwesum"; return mystring} by this way your object is only construct when needed
Can someone explain why using static const strings in the manner that I do so above, risks throwing exceptions ?
See Question&Answers more detail:os