5

I'm sorry if this question gets reported but I can't seem to easily find a solution online. If I override operator()() what behavior does this define?

Josh Elias
  • 3,250
  • 7
  • 42
  • 73
  • 4
    It's the function call operator. You might find this useful: http://stackoverflow.com/questions/4421706/operator-overloading – chris Dec 13 '12 at 05:04

1 Answers1

8

The operator() is the function call operator, i.e., you can use an object of the corresponding type as a function object. The second set of parenthesis contains the list of arguments (as usual) which is empty. For example:

struct foo {
    int operator()() { return 17; };
};

int main() {
    foo f;
    return f(); // use object like a function
}

The above example just shows how the operator is declared and called. A realistic use would probably access member variables in the operator. Function object are used in many places in the standard C++ library as customization points. The advantage of using an object rather than a function pointer is that the function object can have data attached to it.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380