-8

from Wikipedia - Function object

A typical use of a function object is in writing callback functions. A callback in procedural languages, such as C, may be performed by using function pointers.[2] However it can be difficult or awkward to pass a state into or out of the callback function. This restriction also inhibits more dynamic behavior of the function. A function object solves those problems since the function is really a façade for a full object, carrying its own state.

  1. Why do function pointers make it difficult to pass a state into or out of the callback function and dynamic behavior of the function?

  2. if function do not use function pointer how programs can called function

anatolyg
  • 26,506
  • 9
  • 60
  • 134
JPH
  • 9
  • 7
  • 7
    Keep reading your C++ book. When you learn about lambdas, and lambda captures, all will be explained. – Sam Varshavchik Sep 07 '16 at 10:41
  • 3
    Please clarify: "if function do not use function pointer how programs can called function" - the bad grammar makes it hard to understand what you are asking. People can fix grammar, but first they need to know what you are really asking. – anatolyg Sep 07 '16 at 10:41
  • Calling just a function pointer which uses variables can be tricky because the variables might change while executing the function. To work around this issue a function object copies the state of the variables which are being used into it's own object. – Stefan Sep 07 '16 at 10:45
  • 1
    in general this is an interesting question. But your grammar makes it hard to read. And also as mentioned you stumble across function-object when you learn a bit more about c++. – Hayt Sep 07 '16 at 10:45
  • 1. Try to pass state into a function using a function pointer and compare the resulting program with another that uses a closure. Which do you find more awkward? – eerorika Sep 07 '16 at 10:49
  • I this the question OOP vs procedural programming? It is not depending on callbacks, it is depending on objects or not. Sounds like a XY problem description. As written in Wikipedia "A function object solves those problems". And this is not depending on function pointers or any other implementation detail but on using `objects` instead of plain functions and separated data. And this is also is not dependend on the used language. Objects can also be implemented in C or asm. – Klaus Sep 07 '16 at 10:50
  • Are you asking about `C` or `C++`? Those two are not one and the same, and support different idioms – StoryTeller - Unslander Monica Sep 07 '16 at 10:52
  • @Klaus No, it is about using function objects as callbacks, as opposed to using plain function pointers with no state. – juanchopanza Sep 07 '16 at 10:58
  • @juanchopanza: Calling a function or a function object directly or as callback makes no difference. That a call to a function ( directly or via pointer ) can not hold any state is the underlaying problem. Any implementation which points to any kind of an object with state ( data ) while calling the function directly ( eg. this pointer in c++ ) or function object with e.g. operator() can keep the state. So my understanding is simply OOP or not OOP. If it is a function pointer, a vtable offset, called directly or by callback is not important for the question how to keep the state. – Klaus Sep 07 '16 at 11:05
  • @Klaus I don't see the connection with OOP or vtables but whatever. – juanchopanza Sep 07 '16 at 11:42
  • I think this belongs to the programmers stack exchange. – Ivan Rubinson Sep 07 '16 at 12:05
  • 1
    @IvanRubinson when referring other sites, it is often helpful to point that [cross-posting is frowned upon](http://meta.stackexchange.com/tags/cross-posting/info) – gnat Sep 07 '16 at 13:07

1 Answers1

3
  1. As far as function pointers in C go, you need to pass the state as an extra parameter. That's in opposition to languages like python, javascript or C++ that have closures. And that means storage duration needs to be considered for that extra structure.

  2. C programmers do make use of function pointers. And you can't write very generic code without using function pointers. We just apply discipline and pass the captured state in a structure, as a function parameter.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458