function pointer
TRANSCRIPT
![Page 1: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/1.jpg)
Function Pointer
CallbackDelegation
2011/02/15 - Browny
![Page 2: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/2.jpg)
Outline
• Introduction• The Syntax of Function Pointers• Callback Functions in C and C++• Functor• Observer Pattern & Delegation
![Page 3: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/3.jpg)
Introduction
Function Pointer Are
pointer which
point to the address of a function
![Page 4: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/4.jpg)
The Syntax (1)
• 2 diff type function pointerso Pointers to ordinary C function / static C++
member functiono 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.
![Page 5: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/5.jpg)
The Syntax (2)
• Define function pointer
![Page 6: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/6.jpg)
The Syntax (3)
• Assign a address to a function pointer
![Page 7: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/7.jpg)
The Syntax (4)
• Calling a Function using a Function Pointer
![Page 8: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/8.jpg)
The Syntax (5)
• How to Pass a Function Pointer as an Argument
![Page 9: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/9.jpg)
Callback Functions (1)
• What is ?o A callback function is a function that is called
through a function pointer
• Why use callback functions?o They uncouple the caller from the callee (The
caller doesn't care who the callee is)o Ex: A function sorts the items of a field according
to a user-specified ranking.
![Page 10: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/10.jpg)
Callback Functions (2)
• Callback to a non-static C++ Member Function o Different and incompatible signatureso Change the caller function pointer argument to be
compatible ? ( uncoupling fails! )• Solution
oWrite a static member function as a wrapper function (compatible signature)
o Additional argument void*, then cast it to the object on which you want to invoke
![Page 11: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/11.jpg)
Class Declaration
Wrapper function Implementation
Additional void* argument
Casting
Non-static member function
![Page 12: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/12.jpg)
Function Implies a Callback
Main
![Page 13: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/13.jpg)
Problem I met• C++ 會區分兩種類型的成員函數:靜態成員函數
和非靜態成員函數。這兩者之間的一個重大區別是,靜態成員函數不接受隱含的 this 自變量。所以,它就無法訪問自己類的非靜態成員。
• 在某些條件下,比如說在使用諸如 pthread(它不支持類)此類的多線程庫時,就必須使用靜態的成員函數,因為其地址同 C 語言函數的地址兼容。這種限制就迫使程序員要利用各種解決辦法才能夠從靜態成員函數訪問到非靜態數據成員。
![Page 14: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/14.jpg)
Functor
• What is ?o Functors are functions with a stateoWith an overloaded operator () to act like as a
functiono Encapsulate C and C++ function pointers
employing the concepts templates and polymorphism
o Ref. http://kheresy.wordpress.com/2010/11/09/function_object/
![Page 15: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/15.jpg)
Observer Pattern (1)
![Page 16: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/16.jpg)
Observer Pattern (2)
• 觀察者模式定義了一種一對多的依賴關係,讓多個觀察者物件同時監聽某一個主題物件。這個主題物件在狀態發生變化時,會通知所有觀察者物件,使他們自動更新自己。
![Page 17: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/17.jpg)
Observer Pattern (3)
• 當一個物件的改變需要同時改變其他物件,而且他不知道到底有多少物件有待改變時,應該考慮使用觀察者模式
• 觀察者模式在解除耦合。讓耦合的雙方都依賴抽象,而非具體。使各自的變化都部會影響到另一邊的變化。
![Page 18: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/18.jpg)
Delegate
• 缺點:抽象通知者還是依賴抽象觀察者。另外,每個具體觀察者的“反應”函數不一定相同
• 如果通知者和觀察者之間互相不知道,那麼就由用戶端決定通知誰 ( 應用 function pointer, 委派(Delegation) 的概念 )
![Page 19: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/19.jpg)
Reference• The Function Pointer Tutorial :
http://www.newty.de/fpt/index.html
• Callback Functions Tutorial - CodeGuru : http://www.codeguru.com/cpp/cpp/cpp_mfc/callbacks/article.php/c10557
• Observer Pattern : http://en.wikipedia.org/wiki/Observer_pattern
![Page 20: Function pointer](https://reader036.vdocuments.us/reader036/viewer/2022082416/555ad86ad8b42a024a8b4da6/html5/thumbnails/20.jpg)
Thank You Q&A