These kinds of questions tend to reinforce my determination to follow Dan Saks’s advice in this article, Conversations with the Guru: http://www.drdobbs.com/conversationsa-midsummer-nights-madness/184403835 .
In particular, it is about how const placement changes the situation. (*) I understand that I’m unlikely to translate someone from writing const int ...
to int const ...
, but this suggests that I prefer to do the latter.
(*), which states that replacing a constant with a type declaration at the very beginning is one change that has no effect.
This makes reading easier, because for any instance of the word const
in the declaration, everything to the left of it is a type of what is a constant, and everything to the right is what is actually a constant.
Consider a declaration like:
int const * * const pointerToPointer;
The first const
states that the integers at the end of the chain of pointers are constants and can be found in * * const pointerToPointer
. Meanwhile, the second indicates that an object of type pointer to a pointer to const int is also const and that this object is pointerToPointer
.
In the case of OP:
int const MyClass::showName(string id){ ... }
The const type is int, and const is the return value from the function.
Topics:
int MyClass::showName(string id) const { ... }
Here, the type const is a function (string) that returns int, and const is the function itself, i.e. body function.
source share