Problems with understanding the following snippet

Hello everyone,

I am really appreciated if someone explain the meaning of line 10 in the following snippet to me. If operator is a member function of comp so why there are two pair of parenthesis in front of the operator ?!.

Thanks in advance,
A.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Student{
public:
int num;
string name;
};

class Comp
{
public:
bool operator()(Student s1, Student s2)
{
if(s1.num < s2.num )
return true;
else
return false;
}
};
Looks like overloading of the parenthesis (i.e. brackets);

http://www.learncpp.com/cpp-tutorial/99-overloading-the-parenthesis-operator/
it looks like you could use a overloaded stream operator to have a similar outcome. at least in the article it does. not the code above.

I dont like what the example above is doing. i just wouldnt use that particular operator in that way. i think id try and use a template instead. but im just a beginner so I make mistakes.
Last edited on
closed account (z05DSL3A)
acorn,

it is a function object (AKA functor) and could be used in a std::Algorithm such as sort
sort (studentVector.begin(), studentVector.end(), Comp() );

http://en.wikipedia.org/wiki/Function_object
http://www.cplusplus.com/reference/algorithm/sort/
I will look at it Grey Wolf thanks. I appreciate it. seeing how its used makes more sense now.

*edit - hey thats pretty cool.
Last edited on
Thank you very much :-) I've got the meaning by the nice matrix example in the article Moschops.
Last edited on
Topic archived. No new replies allowed.