Lecture04-1

Lecture04-1 - Functor Motivation class student cfw_private:...

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

View Full Document Right Arrow Icon
CS2134 Functor Motivation class student {private:     string name;     double gpa;    . ..   public:      string get_name();      double get_gpa();      operator<(student rhs){ . ..};   . .. } template<class Comparable> {  Comparable maxIndex = 0;     . ..         if (a[maxindex] < a[i]) . .. } Meaning of student::operator< fixed at compile time. What if we want to compare students by name and by gpa at different points within the same program?
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS2134 Functors (Function Objects) Class with no data members and a single method Can pass a functor as a template parameter, just like other classes This effectively passes the member function as a parameter Example: pass LessThanByGPA or LessThanByName to findmax
Background image of page 2
CS2134 // Generic findMax, with a function object. // Precondition: a.size( ) > 0. // class Comparator functor with method IsLessThan template <class Object, class Comparator>                          Comparator comp  ) {   int maxIndex = 0;
Background image of page 3

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

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

Page1 / 12

Lecture04-1 - Functor Motivation class student cfw_private:...

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

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