Reputation: 118
I have a class source file Foo.cpp
and I need to define IntVal
constant only for local use by class methods.
// Option 1
const int IntVal = 5;
// Option 2
static const int IntVal = 5;
// Option 3
namespace {
const int IntVal = 5;
}
int Foo::GetValue()
{
return this->value + IntVal;
}
Which one is preferred?
Upvotes: 0
Views: 166
Reputation: 1260
For hardcoded constants like that, I'd pretty much always go with:
static constexpr int kIntValue = 5;
If I had to bet, I'd say all of those compile to almost exactly the same code though (unless you're trying to use the number in a constexpr context) so it probably comes down to personal preference/style.
Upvotes: 1