#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).
This means that the parameter x is a char whose value cannot be changed inside the function. For example:
void myfunc(const char x)
{
char y = x; // OK
x = y; // failure - x is `const`
}
For the last one:
int myfunc() const;
This is illegal unless it's inside a class declaration - const member functions prevent modification of any class member - const nonmember functions cannot be used. in this case the definition would be something like:
int myclass::myfunc() const
{
// do stuff that leaves members unchanged
}
If you have specific class members that need to be modifiable in const member functions, you can declare them mutable. An example would be a member lock_guard that makes the class's const and non-const member functions threadsafe, but must change during its own internal operation.
Best Answer
It's easier to understand if you rewrite that as the completely equivalent
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 isconst
(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 isconst
.Putting it all together, you can read this as a
const
member function namedMethod3
that takes a reference to aconst
pointer to anint const
(or aconst int
, if you prefer) and returns aconst
pointer to anint const
(const int
).(N.b. #2 is entirely superfluous.)