the other code is not really important, i just wanna know what does the & before the findmax function means and how does it work? i know for the other parts, it meanlys pass by reference like the & a, but i don't know what if does here for a function and how it works.
i'm just asking about the syntax, like i said the code is not important, i just wanted to know what the & findmax() means, i've need seen a & then a space then a function name like & findmax() before so i don't know what it means.
The & belongs to the return type. The return type of the function is const Comparable&. The function returns a reference to the maximum Comparable object of the vector.
That would mean exactly the same. As long as there's no ambiguity, then you can be flexible with the white space.
There is no syntactical construction in C++ of the format type & functionName() in which the & would be associated with the function name. The only possible interpretation is that the & is associated with the return type.
The & belongs to findmax from the point of view of the grammar, and if you were crazy enough to declare more than one function on the same line, it would matter:
1 2 3 4 5 6
const Comparable foo, &findmax(), arr[3], &rfoo = foo, *findmaxp();
// foo is a const Comparable
// findmax is a function returning a reference to a const Comparable
// arr is an array of three const Comparables
// rfoo is a reference to const Comparable
// findmaxp() is a function returning a ponter to a const Comparable
But logically, & is part of the return type, so many C++ programmers put the space after the & to make that clear: const Comparable& findmax();
To the language, the spacing doesn't matter at all.