int& func (const int& var)
^^^^^^^^^^^^^^
The highlighted part is an argument declaration. The part const int&
is the type of the argument variable and var
is the name of the variable.
Normally, constness applies to the left, but in this case, it is the left most token of the type. In this exceptional case, it applies to the right instead. To the right is int
. Therefore it is a const int. As a whole, the type of the argument is reference to const int.
int const func (int& var)
^^^^^^^^^
The highlighted part is the return type declaration of the function. The return type is a const int object. Although it is well-formed, it never really makes sense to return a const int since the constness is irrelevant to the caller. Most compilers have options to warn in case of such declaration.
Technically, returning a const class object may be different from returning a non-const one, but I have not seen a case where that would be useful.
int& func (int& var) const
Const after the argument list applies to the function itself. A const member function may not be called on a non-const object or reference. The implicit *this
argument of the member function will be const. Const qualifier cannot be applied on a non-member function or a static member function.
Regarding old version of the question...
What is the difference between this three functions regarding using "const" modifier
int& func (int& const var)
int& const func (int& var)
These two are ill-formed. Const qualifier cannot apply to a reference (although, you can have a reference to a const type, and such references are colloquially called const references).