global static const - c++
hi
I'm trying to get to a the bottom of how things work with globally defined static variables. I define a .h file with Code:
namespace outerspace My main issue is regarding the memory usage, is there a single memory being located for all the object files including this header file or will it be replicated each time? Hope you can assist regards Marri |
I don't think they're replicated, but I'd go for the following solution, where, I'm sure, they're not replicated:
Code:
class outerspace |
they are replicated
The strings are in fact replicated.
I would suggest the following. const.h Code:
namespace foo Code:
#include "const.h" |
Isn't
Code:
class outerspace 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 |
Quote:
When I did that g++ complained like crazy. Code:
const.h:6: error: invalid in-class initialization of static data member of non-integral type 'const std::string' 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. |
Quote:
|
All times are GMT -5. The time now is 03:50 AM. |