Originally Posted by lorebett
static const std::string FOO_STRING = "foo";
static const std::string BAR_STRING = "bar";
more object oriented?
and it does not require to split declaration and definition in two files...
moreover you access, e.g., FOO_STRING in the same way as for namespaces... outerspace::FOO_STRING
First, have you tried sticking the code above into a header file, including that header file in two source files, then compile them and link them into a program?
When I did that g++ complained like crazy.
const.h:6: error: invalid in-class initialization of static data member of non-integral type 'const std::string'
The way I solved it was by sticking the definition of the constants into a source file. So, I ended up splitting declaration and definition anyway.
Second, I'd argue there's nothing object oriented with global constants. Personally I stick global constants in a class, like you do above, when those constants are related to a class. If the global constants have nothing to do with a class then I stick them in a well named namespace.
I think that a few years ago, before namespaces were properly supported in all major C++ compilers, it was common to "mis-use" classes to hold global constants. This was just to avoid polluting the global namespace. Nowadays namespaces are supported and can be used instead.