Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does operator()() define?

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?

like image 774
Josh Elias Avatar asked Dec 13 '12 05:12

Josh Elias


1 Answers

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.

like image 128
Dietmar Kühl Avatar answered Oct 22 '22 14:10

Dietmar Kühl