Overloading () operator

Hey guys,
So about a week ago I was a bit stuck on how to number of comparisons a particular program makes and asked for help here (http://www.cplusplus.com/forum/beginner/129822/ )
Someone suggested the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct myCompare {
  bool operator ()(int i, int j, int comparisonMethod) {
        counter++;
        switch (comparisonMethod) {
            case 0: return i == j;
            case 1: return i > j;
            case 2: return i >= j;
            case 3: return i < j;
            case 4: return i <= j;
        }
        return false;
  }
    
    int count() const { return counter; }
    private:
    static int counter;
}compare;

int myCompare::counter = 0;

int main()
{
    compare(3, 4, 1);
    return 0;
}


At that time I basically had no clue on classes/structs and overloading. Now after reading up a bit on them I have some questions.
What is the point of overloading the () operator here? Could you not just make another function to do the same thing. Does overloading the () operator give any advantage? Also what exactly is the point of having private and public parts of a class?
Google around "functors" (or "function object" -- we're programming, not doing math).

A functor is like a function, but it keeps private state. Which is very cool and useful.

Hope this helps.
Thanks Duoas
Topic archived. No new replies allowed.