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?
Asked
Active
Viewed 126 times
5
-
4It'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 Answers
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
-
-
@dreamlax: I added a brief description but for an explanation of what function objects are used for you'd look for in existing answers. – Dietmar Kühl Dec 13 '12 at 05:23