C++ Const Usage Explanation
Read this: https://isocpp.org/wiki/faq/const-correctness
The final const
means that the function Method3
does not modify the non mutable members of its class.
const int* const
means a constant pointer to a constant int: i.e. a pointer that cannot be changed, to an int that cannot be changed: the only difference between this and const int&
is that it can be null
const int* const&
means a reference to a constant pointer to a constant int. Usually pointers are not passed by reference; const int* &
makes more sense because it would mean that the pointer could be changed during the method call, which would be the only reason I can see to pass a pointer by reference, const int* const&
is to all intents and purposes the same as const int* const
except that it is probably less efficient as pointers are plain old data (POD) types and these should, in general be passed by value.
It's easier to understand if you rewrite that as the completely equivalent
// v───v───v───v───v───v───v───v───v───v───v───v─┬┐
// ││
// v──#1 v─#2 v──#3 v─#4 #5
int const * const Method3(int const * const&) const;
then read it from right to left.
#5 says that the entire function declaration to the left is const
, which implies that this is necessarily a member function rather than a free function.
#4 says that the pointer to the left is const
(may not be changed to point to a different address).
#3 says that the int
to the left is const
(may not be changed to have a different value).
#2 says that the pointer to the left is const
.
#1 says that the int
to the left is const
.
Putting it all together, you can read this as a const
member function named Method3
that takes a reference to a const
pointer to an int const
(or a const int
, if you prefer) and returns a const
pointer to an int const
(const int
).
(N.b. #2 is entirely superfluous.)