I’m trying to decide what the best option would be for when an object has some traits that won’t change, and are needed throughout its functions.
- Static const members
- Const members
It seems to me like the real reason for a static member is to have a variable that can be changed, and thus affect all other objects of the same class. However, I’ve had people recommend class “invariants” to be static const members. I’m looking for some insight regarding the recommended approach to establishing class constants, and reasons why.
“Won’t change” is not precise enough. The main question here is whether different objects of the class need to have different values of these const members (even if they don’t change during the object’s lifetime) or all objects should use (share) the same value.
If the value is the same for all objects of the class, then, of course, it should be a
static constmember of the class.If different objects might require different values, then it should be just a non-static
constmember.