Yahoo Interview Question
Software Engineer / DevelopersAns. Functors are functions with a state. In C++ you can realize them as a class with one or more private members to store the state and with an overloaded operator () to execute the function. Functors can encapsulate C and C++ function pointers employing the concepts templates and polymorphism. You can build up a list of pointers to member functions of arbitrary classes and call them all through the same interface without bothering about their class or the need of a pointer to an instance. All the functions just have got to have the same return-type and calling parameters. Sometimes functors are also known as closures. You can also use functors to implement callbacks.
Function Pointers are pointers, i.e. variables, which point to the address of a function. You must keep in mind, that a running program gets a certain space in the main-memory. Both, the executable compiled program code and the used variables, are put inside this memory. Thus a function in the program code is, like e.g. a character field, nothing else than an address. It is only important how you, or better your compiler/processor, interpret the memory a pointer points to.
Function Pointers provide the concept of callback functions. A callback is done just like a normal function call would be done: You just use the name of the function pointer instead of a function name.
1. functors are class objects which overload operator(),
- lapak2009 December 22, 20092. function pointers are pointer to function, eg: vtbl is a array of function pointers...
3. callback functions are function pointers passed as parameters of function, which can be called back when an event occurs...eg: when a thread is created,we send a callback function to be called when thread starts...