Function PointerCallbackDelegation<br />2011/02/15 - Browny<br />
Outline<br />Introduction<br />The Syntax of Function Pointers<br />Callback Functions in C and C++<br />Functor<br />Obse...
Introduction<br />Function Pointer <br />Are<br />pointer <br />which<br />point to the address of a function<br />
The Syntax (1)<br />2 diff type function pointers<br />Pointers to ordinary C function / static C++ member function<br />P...
The Syntax (2)<br />Define function pointer<br />
The Syntax (3)<br />Assign a address to a function pointer<br />
The Syntax (4)<br />Calling a Function using a Function Pointer<br />
The Syntax (5)<br />How to Pass a Function Pointer as an Argument <br />
Callback Functions (1) <br />What is ?<br />A callback function is a function that is called through a function pointer<br...
Callback Functions (2) <br />Callback to a non-static C++ Member Function <br />Different and incompatible signatures<br /...
Class Declaration<br />Non-static member function <br />Wrapper function Implementation<br />Additional void* argument<br ...
Function Implies a Callback<br />Main<br />
Problem I met<br />C++會區分兩種類型的成員函數:靜態成員函數和非靜態成員函數。這兩者之間的一個重大區別是,靜態成員函數不接受隱含的this自變量。所以,它就無法訪問自己類的非靜態成員。<br />在某些條件下,比如說在使用...
Functor<br />What is ?<br />Functors are functions with a state<br />With an overloaded operator () to act like as a funct...
Observer Pattern (1)<br />
Observer Pattern (2)<br />觀察者模式定義了一種一對多的依賴關係,讓多個觀察者物件同時監聽某一個主題物件。這個主題物件在狀態發生變化時,會通知所有觀察者物件,使他們自動更新自己。<br />
Observer Pattern (3)<br />當一個物件的改變需要同時改變其他物件,而且他不知道到底有多少物件有待改變時,應該考慮使用觀察者模式<br />觀察者模式在解除耦合。讓耦合的雙方都依賴抽象,而非具體。使各自的變化都部會影響到另...
Delegate<br />缺點:抽象通知者還是依賴抽象觀察者。另外,每個具體觀察者的“反應”函數不一定相同<br />如果通知者和觀察者之間互相不知道,那麼就由用戶端決定通知誰 (應用 function pointer, 委派(Delegat...
Reference<br />The Function Pointer Tutorial : http://www.newty.de/fpt/index.html<br />Callback Functions Tutorial - CodeG...
Thank You Q&A<br />
Upcoming SlideShare
Loading in...5
×

Function pointer

1,352

Published on

Published in: Education, Lifestyle, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,352
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Function pointer

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

    Clipping is a handy way to collect important slides you want to go back to later.

×