SlideShare une entreprise Scribd logo
1  sur  20
Function PointerCallbackDelegation 2011/02/15 - Browny
Outline Introduction The Syntax of Function Pointers Callback Functions in C and C++ Functor Observer Pattern & Delegation
Introduction Function Pointer  Are pointer  which point to the address of a function
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.
The Syntax (2) Define function pointer
The Syntax (3) Assign a address to a function pointer
The Syntax (4) Calling a Function using a Function Pointer
The Syntax (5) How to Pass a Function Pointer as an Argument
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.
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
Class Declaration Non-static member function  Wrapper function Implementation Additional void* argument Casting
Function Implies a Callback Main
Problem I met C++會區分兩種類型的成員函數:靜態成員函數和非靜態成員函數。這兩者之間的一個重大區別是,靜態成員函數不接受隱含的this自變量。所以,它就無法訪問自己類的非靜態成員。 在某些條件下,比如說在使用諸如 pthread(它不支持類)此類的多線程庫時,就必須使用靜態的成員函數,因為其地址同C語言函數的地址兼容。這種限制就迫使程序員要利用各種解決辦法才能夠從靜態成員函數訪問到非靜態數據成員。
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/
Observer Pattern (1)
Observer Pattern (2) 觀察者模式定義了一種一對多的依賴關係,讓多個觀察者物件同時監聽某一個主題物件。這個主題物件在狀態發生變化時,會通知所有觀察者物件,使他們自動更新自己。
Observer Pattern (3) 當一個物件的改變需要同時改變其他物件,而且他不知道到底有多少物件有待改變時,應該考慮使用觀察者模式 觀察者模式在解除耦合。讓耦合的雙方都依賴抽象,而非具體。使各自的變化都部會影響到另一邊的變化。
Delegate 缺點:抽象通知者還是依賴抽象觀察者。另外,每個具體觀察者的“反應”函數不一定相同 如果通知者和觀察者之間互相不知道,那麼就由用戶端決定通知誰 (應用 function pointer, 委派(Delegation) 的概念)
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
Thank You Q&A

Contenu connexe

Tendances

Tendances (20)

Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
Function in C++
Function in C++Function in C++
Function in C++
 
Notes part5
Notes part5Notes part5
Notes part5
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Functions part1
Functions part1Functions part1
Functions part1
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Functions in C
Functions in CFunctions in C
Functions in C
 
What's New In Python 2.5
What's New In Python 2.5What's New In Python 2.5
What's New In Python 2.5
 
Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/
 
4. function
4. function4. function
4. function
 
polymorphism and virtual function
polymorphism and virtual functionpolymorphism and virtual function
polymorphism and virtual function
 
What's New In Python 2.4
What's New In Python 2.4What's New In Python 2.4
What's New In Python 2.4
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
c.p function
c.p functionc.p function
c.p function
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
Function Parameters
Function ParametersFunction Parameters
Function Parameters
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Similaire à Function pointer

Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
nirajmandaliya
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
miki304759
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
Alisha Korpal
 

Similaire à Function pointer (20)

Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++
 
Function different types of funtion
Function different types of funtionFunction different types of funtion
Function different types of funtion
 
Functions
FunctionsFunctions
Functions
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothi
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
 
DS Unit 6.ppt
DS Unit 6.pptDS Unit 6.ppt
DS Unit 6.ppt
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
 

Plus de Shih-Hsiang Lin (13)

Introduction to Apache Ant
Introduction to Apache AntIntroduction to Apache Ant
Introduction to Apache Ant
 
Introduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageIntroduction to GNU Make Programming Language
Introduction to GNU Make Programming Language
 
Ch6 file, saving states, and preferences
Ch6 file, saving states, and preferencesCh6 file, saving states, and preferences
Ch6 file, saving states, and preferences
 
[C++ gui programming with qt4] chap9
[C++ gui programming with qt4] chap9[C++ gui programming with qt4] chap9
[C++ gui programming with qt4] chap9
 
Ch5 intent broadcast receivers adapters and internet
Ch5 intent broadcast receivers adapters and internetCh5 intent broadcast receivers adapters and internet
Ch5 intent broadcast receivers adapters and internet
 
Ch4 creating user interfaces
Ch4 creating user interfacesCh4 creating user interfaces
Ch4 creating user interfaces
 
Ch3 creating application and activities
Ch3 creating application and activitiesCh3 creating application and activities
Ch3 creating application and activities
 
[C++ GUI Programming with Qt4] chap7
[C++ GUI Programming with Qt4] chap7[C++ GUI Programming with Qt4] chap7
[C++ GUI Programming with Qt4] chap7
 
[C++ GUI Programming with Qt4] chap4
[C++ GUI Programming with Qt4] chap4[C++ GUI Programming with Qt4] chap4
[C++ GUI Programming with Qt4] chap4
 
Introduction to homography
Introduction to homographyIntroduction to homography
Introduction to homography
 
Git basic
Git basicGit basic
Git basic
 
Project Hosting by Google
Project Hosting by GoogleProject Hosting by Google
Project Hosting by Google
 
An Introduction to Hidden Markov Model
An Introduction to Hidden Markov ModelAn Introduction to Hidden Markov Model
An Introduction to Hidden Markov Model
 

Dernier

Dernier (20)

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 

Function pointer

  • 2. Outline Introduction The Syntax of Function Pointers Callback Functions in C and C++ Functor Observer Pattern & Delegation
  • 3. Introduction Function Pointer Are pointer which point to the address of a function
  • 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.
  • 5. The Syntax (2) Define function pointer
  • 6. The Syntax (3) Assign a address to a function pointer
  • 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
  • 11. Class Declaration Non-static member function Wrapper function Implementation Additional void* argument Casting
  • 12. Function Implies a Callback Main
  • 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/
  • 16. Observer Pattern (2) 觀察者模式定義了一種一對多的依賴關係,讓多個觀察者物件同時監聽某一個主題物件。這個主題物件在狀態發生變化時,會通知所有觀察者物件,使他們自動更新自己。
  • 17. Observer Pattern (3) 當一個物件的改變需要同時改變其他物件,而且他不知道到底有多少物件有待改變時,應該考慮使用觀察者模式 觀察者模式在解除耦合。讓耦合的雙方都依賴抽象,而非具體。使各自的變化都部會影響到另一邊的變化。
  • 19. 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