4. The Syntax (1) 2 diff type function pointers Pointers to ordinary C function / static C++ member function Pointers to non-static C++ member function (need a hidden argument: this-pointer of an instance of the class) These 2 types of function pointers are incompatible with each other.
7. The Syntax (4) Calling a Function using a Function Pointer
8. The Syntax (5) How to Pass a Function Pointer as an Argument
9. Callback Functions (1) What is ? A callback function is a function that is called through a function pointer Why use callback functions? They uncouple the caller from the callee (The caller doesn't care who the callee is) Ex: A function sorts the items of a field according to a user-specified ranking.
10. Callback Functions (2) Callback to a non-static C++ Member Function Different and incompatible signatures Change the caller function pointer argument to be compatible ? ( uncoupling fails! ) Solution Write a static member function as a wrapper function (compatible signature) Additional argument void*, then cast it to the object on which you want to invoke
13. Problem I met C++會區分兩種類型的成員函數:靜態成員函數和非靜態成員函數。這兩者之間的一個重大區別是,靜態成員函數不接受隱含的this自變量。所以,它就無法訪問自己類的非靜態成員。 在某些條件下,比如說在使用諸如 pthread(它不支持類)此類的多線程庫時,就必須使用靜態的成員函數,因為其地址同C語言函數的地址兼容。這種限制就迫使程序員要利用各種解決辦法才能夠從靜態成員函數訪問到非靜態數據成員。
14. Functor What is ? Functors are functions with a state With an overloaded operator () to act like as a function Encapsulate C and C++ function pointers employing the concepts templates and polymorphism Ref. http://kheresy.wordpress.com/2010/11/09/function_object/