24+-+Functors - EECS 280 Programming and Introductory Data...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Functors EECS 280 Programming and Introductory Data Structures 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
First-class Objects What are they? 2 There are lots of things we can do with integers in a running program, such as: Create them Destroy them Pass them as arguments Return them as values In fact, we can do these four things with all values of the simplest types in the language – even types we create ourselves with the enum, struct, or class mechanisms.
Background image of page 2
First-class Objects What they aren’t 3 Unlike values, types and functions are not first class objects in C++; but, they can sometimes come close. The template mechanism allows us to pass types as arguments. The function pointer mechanism allows us to pass functions as arguments and return them as results. Specifically, we cannot create and destroy
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Function Objects Motivation 4 We'll illustrate this problem by using the list_t ADT from project 2 to write a generic "has-a" function using function pointers. Remember the operations on a list_t : list_t list_make(int v, list_t l); int list_first(list_t l); list_t list_rest(list_t l);
Background image of page 4
Function Objects Motivation 5 Given these list operations, suppose we wanted to write the following routine: bool has_odd(list_t l); // EFFECT: returns true if l has // an odd element, false otherwise The recursive definition of this function is: The empty list has no odd elements A non-empty list has odd elements if:
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Function Objects Motivation 6 So, the recursive definition of this function is: bool has_odd(list_t l) { if (list_isEmpty(l)) { return false; } else if (list_first(l) % 2) { return true; }
Background image of page 6
Function Objects Motivation 7 If we write has_even , it looks almost literally the same: bool has_even(list_t l) { if (list_isEmpty(l)) { return false; } else if (!(list_first(l) % 2)) { return true; }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Function Objects Motivation 8 If we write has_even , it looks almost literally the same: bool has_even(list_t l) { if (list_isEmpty(l)) { return false; } else if (!(list_first(l) % 2)) { return true; } We can abstract away the "predicate" as a function and generalize over it.
Background image of page 8
Motivation 9 The generic function header would be something like this: bool has_a(list_t l, bool (*pred)(int)) This says: " has_a takes two arguments,
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/19/2012 for the course EECS 280 taught by Professor Noble during the Winter '08 term at University of Michigan.

Page1 / 34

24+-+Functors - EECS 280 Programming and Introductory Data...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online