newbie here!
I am reading a code and I see the author frequently writing a member function as
const int func (const scalar& a) const
// etc
You see there are three const here, now I understand the middle one, const scalar& a, that aims to not change the object a, but what about the other two const?
Is it a good habit that I should do this all the time, to protect blahblah unchanged?
Thanks a lot!
The code you posted is not valid, it will not compile. However, if you consider
The first
constwill specify that the return value is constant (i.e. immutable). The secondconst(const scalar& a) specifies that the function does not modify the value of the value of the argumenta. The third const specifies thatfuncis a constant member function, i.e. it does not modify theMyClassinstance itself.