'&$%CSE 303:Concepts and Tools for Software DevelopmentHal PerkinsAutumn 2008Lecture 29— Function Pointers and ObjectsCSE303 Autumn 2008, Lecture 29 1'&$%Function pointers“Pointers to code” are almost as useful as “pointers to data”.(But the syntax is more painful.)(Somewhat silly) example:void app_arr(int len, int * arr, int (*f)(int)) {for(; len > 0; --len)arr[len-1] = (*f)(arr[len-1]);}int twoX(int i) { return 2*i; }int sq(int i) { return i*i; }void twoXarr(int len, int* arr) { app_arr(len,arr,&twoX); }void sq_arr(int len, int* arr) { app_arr(len,arr,&sq); }CSE 341 spends a week on why function pointers are s o use ful; todayis mostly just how in C.CSE303 Autumn 2008, Lecture 29 2'&$%Function pointers, cont’dKey computer-science idea: You can pass what code to execute as anargument, just like you pass what data to process as an argument.Java: An object is (a pointer to) code and data, so you’re doing bothall the time.// Javainterface I { int m(int i); }void f(int arr[], I obj) {for(int len=arr.length; len > 0; --len)arr[len-1] = obj.m(arr[len-1]);}The m method of an I can have access to data (in fields).C separates the concepts of code, data, and pointers.CSE303 Autumn 2008, Lecture 29 3'&$%C function-pointer syntaxC syntax: painful and confusing. Rough idea: The compiler “knows”what is code and what is a pointer to code, so you can write less thanwe did on the last slide:arr[len-1] = (*f)(arr[len-1]);→ arr[len-1] = f(arr[len-1]);app_arr(len,arr,&twoX);→ app_arr(len,arr,twoX);For types, let’s pretend you always have to write the “pointer to code”part (i.e., t0 (*)(t1,t2,...,tn)) and for declarations the variableor field name goes after the *.Sigh.CSE303 Autumn 2008, Lecture 29 4'&$%What is an Object?First Aproximation• An object consists of data and methods– Provides the correct model– Easy to explain• But. . .– Doesn’t make engineering sense — we don’t want to replicatethe (same) method bodies (code) in every objectCSE303 Autumn 2008, Lecture 29 5'&$%What is an Object?Second Aproximation• An object consists of data and pointers to methods• The compiler adds an additional, implicit this parameter to everymethod to provide a reference to the receiving object– Gives the method a way to refer to the instance variables ofthe correct receiver object• Avoids code duplication• But. . .– Still wastes s pace, particularly if there is relatively littleinstance data, or if the class has a large number of methodsCSE303 Autumn 2008, Lecture 29 6'&$%What is an Object?How it’s really done• There is a single “virtual function” table (vtable) for each classcontaining pointers to the methods belonging to that class.– This is static class data — does not c hange during execution• An object consists of data and a pointer to its class vtable• Method calls are indirect through the vtable• Each method still has an implicit this parameter that refers tothe receiving object• Avoids code duplication• Avoids method pointer duplication• Costs an indirect pointer lookup for each function callCSE303 Autumn 2008, Lecture 29 7'&$%Inheritance and OverridingBasic ideas:• We have a vtable for every class and subclass• The vtable for a subclass points to the correct methods — eitherones belonging to the base class that are inherited, or onesbelonging to the subclass (added or overriding)• Key idea: The initial part of the vtable for a subclass points to themethods that are inherited or overridden from the base class inexactly the same order they appear in the base c lass vtable– So compiled code can find a method at the same offset in thevtable whether it is overridden or not• Use casts as needed to adjust references up and down theinheritance chainCSE303 Autumn 2008, Lecture 29
View Full Document