what is function object

i came across this statement in a book and i cant understand it please help me out....
the sentence is:The templates in <function> help you construct objects that define operator().These objects are called function objects and can be used in place of function pointers in many places.(statement ends here.)
please tell me what is operator() and explain functio object in above lines.
help me out please....
Last edited on
A function object is basically a struct or a class (an "object") which can be used as a function. This basically means you can have a function with scope, and can do different things based on the object supplied, which can be used to replace function pointers (as the quote states.). Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

// Create a definition for the object
struct funcObject {
    // Make it effectively "void funcObject(void);" as the definition
    void operator() (void) {
        // do something in the "function"
        std::cout << "Hello! << std::endl;
    }
};

int main() {
    funcObject myFuncObject;
    myFuncObject();

    return 0;
} 


This program should output "Hello!".
Topic archived. No new replies allowed.