1 / 17

Function Classes and Objects

Function Classes and Objects. Andy Wang Data Structures, Algorithms, and Generic Programming. Function Objects. Overload operator() Can take any number of parameters Can return any value “Configurable” functions with their own data members class makeuppercase { public:

keon
Download Presentation

Function Classes and Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Function Classes and Objects Andy Wang Data Structures, Algorithms, and Generic Programming

  2. Function Objects • Overload operator() • Can take any number of parameters • Can return any value • “Configurable” functions with their own data members class makeuppercase { public: void operator() (char& c) const { c = toupper(c); } };

  3. Function Objects (2) • Declare a function object like any other class • Evaluate a function object by applying the operator() after the object name class makeuppercase; // forward declaration makeuppercase muc; // a function object char ch; cin >> ch; muc(ch);

  4. Two roles of operator() • Overloading an operator by name • Obtaining input parameters of the operator function

  5. Another Example of a Function Class class wildcard { public: wildcard() : v(‘?’) { } explicit wildcard(char c) : v(c) { } String operator() (const String& S) const; void set(char c) { v = c; } private: char v; }; String wildcard::operator() (const String& S) const { String R(S); for (unsigned int i = 0; i < S.Size(); ++) { if (S[i] == v) { R[i] = ‘?’; } } Return R; }

  6. Another Example of a Function Class (2) #include <iostream.h> #include <xstring.h> int main() { String S(“character”); wildcard k_to_?(‘k’); wildcard i_to_?(‘i’); cout << k_to_?(S) << endl; // “?hara?ter” cout << i_to_?(S) << endl; // “ch?r?cter” }

  7. Advantages of Function Objects • Abstraction • Template parameters • Versatility • Dynamically create functions with different behaviors • Performance • More information available to compilers to generate faster code

  8. More on Function Objects • Most common use in STL: Predicate Classes • Function classes that return a Boolean value • Examples: LessThan<T>, GreaterThan<T> • Other examples • Hash objects • Wrapper for any function to be applied by generic algorithms (e.g., makeuppercase) • IO manipulators • Dynamically configurable functions

  9. Predicate Class TLessThan<T> template <typename T> class TLessThan { public: int operator() (const T& t1, const T& t2) const; }; template <typename T> int TLessThan<T>::operator() (const T& t1, const T& t2) const { return (t1 < t2); }

  10. Predicate Class TGreaterThan<T> template <typename T> class TGreaterThan { public: int operator() (const T& t1, const T& t2) const; }; template <typename T> int TGreaterThan<T>::operator() (const T& t1, const T& t2) const { return (t1 > t2); }

  11. Function Class THashClass<T> template <typename T> class THashClass { public: unsigned int operator() (const T&) const; }; template <typename T> unsigned int THashClass<T>::operator() (const T& t) const { return hash_function(t); }

  12. Simple I/O Manipulators • Use unsigned long y; cout << hex << y; // output y in hex notation • Implementation ios& operator<<(ios& s, ios& F(ios&)) { F(s); return(s); } ios& hex(ios& s) { s.unsetf(ios::oct | ios::dec); s.setf(ios::hex); return s; }

  13. I/O Manipulators with Parameters • Use cout << beep(10); // output 10 “beep”s • Implementation (non-generic) class beep { private; int numbeeps; public: beep(int n): numbeeps(n) { } ostream& operator() (ostream& os) const { for (int i = 0; i < numbeeps; ++i) { os << ‘\a’; } return os; } };

  14. I/O Manipulators with Parameters (2) ostream& os operator<<(ostream& os, beep bp) { bp(os); return os; }

  15. A pointer to a function I/O Manipulators with Parameters Using Templates • Generic one-parameter manipulator class template<class S, typename T> class _STFO { public: _STFO(S& (*function) (S&, T), T Tval) : F(function), t(Tval) { } S& operator() (S& s) const { return (*F) (s, t); } private: S& (*F) (S&, T); T t; };

  16. I/O Manipulators with Parameters Using Templates (2) • Generic overload of left shift operator template<class S, typename T> S& operator<<(S& s, const _STFO<S, T>& fo) { return fo(s); } • Instantiation for beep(n) ostream& _beep(ostream& s, int n) { for (int i = 0; i < n; ++i) { s << ‘\a’; return s; } } _STFO<ostream, int> beep(int n) { return _STFO<ostream, int> ((ostream& (*) (ostream&, int)) _beep, n); }

  17. Generic Overload of Left Shift Operator template<class S, typename T> S& operator<<(S& s, const _STFO<S, T>& fo) { return fo(s); }

More Related