project
play

Project Takeaway submitted. Function Pointers Next submission: - PDF document

Project Takeaway submitted. Function Pointers Next submission: Othello Nov 9 th Project Project Grading guide Grading guide Things I will be looking for in final submission Things I will be looking for in


  1. Project • Takeaway – submitted. Function Pointers • Next submission: – Othello – Nov 9 th Project Project • Grading guide • Grading guide – Things I will be looking for in final submission – Things I will be looking for in final submission • Framework • Othello functionality – No duplicate code – All games “playing nice” together – Not as important as having it use the framework correctly. – Small main() • Style – All games use common framework – RCS usage • Memoization – Nice comments + documentation – Othello must have memoization – NIM should have memoization – Readable code. » If not, your framework best be solid – No poetry! ☺ • Memory Problems – I will be running purify. Project Exam 2 – Making up points • Will be returned tomorrow. • Submission 1 – UML…Expect a complete class diagram • Submission 2 – Fully operational Takeaway – Works within framework • Submission 3 – Fully operational NIM – Works within framework – Questions?

  2. Plan for this week Before we begin • Function pointers • Any questions? • Writing inserters and extractors – Still haven’t forgotten!!! Recall: Program Memory Function pointers • Provides access to executable code section. • The memory used by a program is generally separated into the following sections: • Function Pointers are pointers – Code – Where the executable code is kept – variables, which point to the address of a – Global – Where storage for global variables is kept function. – Stack – Runtime stack (where local variables are kept) – Contains a memory address – Heap – Free store for dynamically allocated variables. • Examples from – Exception – special place for things thrown – http://www.function-pointer.org/ – Yes, function pointers have their own web site Function pointers: but why? Function pointers: but why? // solution with a switch-statement – // the four arithmetic operations // <opCode> specifies which operation to execute // one of these functions is selected at runtime // with a switch or a function pointer void Switch( float a, float b, char opCode) float Plus ( float a, float b) { return a+b; } { float Minus ( float a, float b) { return a-b; } float result; // execute operation switch (opCode) { float Multiply ( float a, float b) { return a*b; } case '+' : result = Plus (a, b); break ; float Divide ( float a, float b) { return a/b; } case '-' : result = Minus (a, b); break ; case '*' : result = Multiply (a, b); break ; case '/' : result = Divide (a, b); break ; } cout << "switch: 2+5=" << result << endl; }

  3. Using function pointers Using function pointers // solution with a function pointer // execute example code // <pt2Func> is a function pointer and points to // a function which takes two floats and returns a void Replace_A_Switch() // float. Thefunction pointer { // "specifies" which operation shall be executed. // '+' specifies function 'Plus' to be executed void Switch_With_Function_Pointer( float a, float b, Switch(2, 5, '+'); float (*pt2Func)( float , float )) { //pointer to function 'Minus // call using function pointer Switch_With_Function_Pointer(2, 5, &Minus); float result = pt2Func(a, b); cout << result << endl; } } Using function pointers Function pointer syntax • int (*pt2Function) (float, char, char) ; • Important note: – A function pointer always points to a function with a specific signature! – all functions, you want to use with the same function pointer, must have the same Name of variable parameters and return-type! Argument types Return holding the memory type address of a function • Questions so far? Function pointer syntax Function pointer syntax • int (*pt2Function) (float, char, char) ; • Note: – int (*pt2Function) (float, char, char) ; – Defines a pointer variable pt2Function – The function that this pointer is pointing to takes a float and 2 chars as arguments • Is not the same as – The function that this pointer is pointing to will return an int. – int *pt2Function (float, char, char) ;

  4. Function pointer syntax Function pointer syntax • Assigning to a function pointer: • Note: – int (*pt2Function) (float, char, char) ; int DoIt ( float a, char b, char c) • Defines a function pointer variable { printf("DoIt\n"); return a+b+c; } int DoMore( float a, char b, char c) – int *pt2Function (float, char, char) ; { printf("DoMore\n"); return a-b+c; } • Defines a function that returns a pointer to an int. int (*pt2Function) (float, char, char) ; pt2Function = DoMore; // assignment pt2Function = &DoIt; // alternative Must have same arguments and return type! Function pointer syntax Function pointer syntax • Can also assign to member functions. • Calling a Function using a Function Pointer class TMyClass { – Can call directly or dereference public : int DoIt ( float a, char b, char c){ return a+b+c; }; int DoMore( float a, char b, char c){ return a- int result1 = pt2Function (12, 'a', 'b'); b+c; }; /* more of TMyClass */ int result2 = (*pt2Function) (12, 'a', 'b'); }; int (TMyClass:: *pt2Function) (float, char, char) ; pt2Function = TMyClass::DoMore; // assignment pt2Function = &TMyClass::DoIt; // alternative Function pointer syntax Function pointer syntax • Once again, return type and args must match: • Can also assign to member functions void (*pf)(string); void f1 (string); int f2 (string); int (TMyClass:: *pt2Function) (float, char, char) ; void f3 (int *); pt2Function = TMyClass::DoMore; // assignment void f() TMyClass A = new TMyClass(); { A->pt2Function (7.7, ‘a’,’b’); // direct call pf = &f1; // okay pf = &f2; // bad return type A->(* pt2Function)(7.7, ‘a’,’b’); // dereference pf = &f3; // bad arg type pf (“Foo”); // okay pf (1); // bad arg type int i = pf (“Zero”); // bad return type; }

  5. Function pointer syntax Function pointer syntax • Passing function pointer to a function • Returning a function pointer // function takes a char and returns a pointer to a // <pt2Func> is a pointer to a function which returns an int // function which is taking two and takes a float and two char void PassPtr( int (*pt2Func)( float , char , char )) // floats and returns a float. { // <opCode> specifies which function to return // call using function pointer float result = pt2Func(12, 'a', 'b'); float (*GetPtr1( const char opCode))( float , float ) } { // execute example code - 'DoIt' is a suitable function if (opCode == '+') return &Plus; void Pass_A_Function_Pointer() if (opCode == '-') return &Minus; { PassPtr(&DoIt); } } Function pointer syntax Function pointer syntax • float (*GetPtr1( const char opCode))( float , • Returning a function pointer float ) • Use typedef to avoid that crazy syntax Actual function name typedef float (*pt2Func)( float , float ); pt2Func GetPtr2( const char opCode) Indicates pointer Args of { to a function is function if (opCode == '+') return &Plus; returned pointed to if (opCode == '-') return &Minus; } Type returned by function pointed to Arrays of function pointers Arrays of function pointers • Since function pointers are just pointers, you can • But why? easily have arrays of them – Let’s assume we have a menu system for a GUI. typedef int (*pt2Function)( float , char , char ); / – Each menu item will correspond to an action. void Array_Of_Function_Pointers() { – Can use array of function pointers rather than a pt2Function funcArr[10]; large switch or if/then statements. funcArr[0] = &DoIt; funcArr[1] = &DoMore; printf("%d\n", funcArr[1](12, 'a', 'b')); printf("%d\n", funcArr[0](12, 'a', 'b')); }

  6. Arrays of function pointers Callbacks typedef void (*MenuF)(); • Function Pointers provide the concept of callback functions. MenuF edit_ops[] = { &cut, &copy, &paste, &find }; MenuF file_ops[] = { &open, &new, &close, &save }; • Example MenuF *button2 = edit_ops; MenuF *button3 = file_ops; typedef void (*terminate_handler)(); terminate_handler set_terminate(terminate_handler); // When selection is made Button2[2](); Questions? Callbacks Callbacks void qsort( ... , int (_USERENTRY *cmpFunc)( const void *, • Consider qsort: const void *)) { /* sort algorithm - note: item1 and item2 are void- void qsort( pointers */ void * field, int bigger=cmpFunc(item1, item2); // make callback size_t nElements, size_t sizeOfAnElement, /* use the result */ int (_USERENTRY *cmpFunc)( const void *, const void *) } ); Callback that defines compare function Callbacks • Of course, if we want to do generic programming, why not use STL? – And functors!! – But more on that tomorrow. – Questions?

Recommend


More recommend