Search code examples
c++class-constants

What is the difference between a constant member and a private member without setter?


Suppose I have a constant member and different object have different values for this constant, what is the difference between this constant member and a private member without a setter?


Solution

  • Apart from cv-qualification and accessibility being two completely different concepts there are also practical implications to const public member vs private non-const member.

    • private members cannot be reached outside of their object so you will have to create a public method (getter) if you want to do that
    • private non-const member is mutable, i.e. methods defined in the same class can change it
    • const member cannot be changed once you initialize it

    It all depends on what you want to do.