20
Function Pointer Callback Delegation 2011/02/15 - Browny

Function pointer

Embed Size (px)

Citation preview

Page 1: Function pointer

Function Pointer

CallbackDelegation

2011/02/15 - Browny

Page 2: Function pointer

Outline

• Introduction• The Syntax of Function Pointers• Callback Functions in C and C++• Functor• Observer Pattern & Delegation

Page 3: Function pointer

Introduction

Function Pointer Are

pointer which

point to the address of a function

Page 4: Function pointer

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

The Syntax (2)

• Define function pointer

Page 6: Function pointer

The Syntax (3)

• Assign a address to a function pointer

Page 7: Function pointer

The Syntax (4)

• Calling a Function using a Function Pointer

Page 8: Function pointer

The Syntax (5)

• How to Pass a Function Pointer as an Argument

Page 9: Function pointer

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

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

Class Declaration

Wrapper function Implementation

Additional void* argument

Casting

Non-static member function

Page 12: Function pointer

Function Implies a Callback

Main

Page 13: Function pointer

Problem I met• C++ 會區分兩種類型的成員函數:靜態成員函數

和非靜態成員函數。這兩者之間的一個重大區別是,靜態成員函數不接受隱含的 this 自變量。所以,它就無法訪問自己類的非靜態成員。

• 在某些條件下,比如說在使用諸如 pthread(它不支持類)此類的多線程庫時,就必須使用靜態的成員函數,因為其地址同 C 語言函數的地址兼容。這種限制就迫使程序員要利用各種解決辦法才能夠從靜態成員函數訪問到非靜態數據成員。

Page 14: Function pointer

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

Observer Pattern (1)

Page 16: Function pointer

Observer Pattern (2)

• 觀察者模式定義了一種一對多的依賴關係,讓多個觀察者物件同時監聽某一個主題物件。這個主題物件在狀態發生變化時,會通知所有觀察者物件,使他們自動更新自己。

Page 17: Function pointer

Observer Pattern (3)

• 當一個物件的改變需要同時改變其他物件,而且他不知道到底有多少物件有待改變時,應該考慮使用觀察者模式

• 觀察者模式在解除耦合。讓耦合的雙方都依賴抽象,而非具體。使各自的變化都部會影響到另一邊的變化。

Page 18: Function pointer

Delegate

• 缺點:抽象通知者還是依賴抽象觀察者。另外,每個具體觀察者的“反應”函數不一定相同

• 如果通知者和觀察者之間互相不知道,那麼就由用戶端決定通知誰 ( 應用 function pointer, 委派(Delegation) 的概念 )

Page 19: Function pointer

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

Thank You Q&A