I have a question about the best way to use constants in C++. There seem to
be variations in the way that they are used, and I am confused as to what I
should be using. Basically since the constant is used in only one class,
should I try to scope it to just that class? to the file?
what are the thoughts out there.
1.)
I have seen people declare in their .h file
static const int myVariable;
And then in their .cpp put the definition.
myVariable = 10;
2.) Is it appropriate if the variable is only to be used in a class to put
that definition in the class so as to scope it to that class only?
umm, but then should it be private with an accessor?
So for example,
class MyClass {
public:
static const int myVariable = 10;
private:
}
3.)
Is it best to put it in a blank namespace in the .cpp file to keep the scope
to that file only?
So for example,
namespace {
static const int myVariable = 10;
}
I appreciate comments on this matter.
thanks for your help.