Title: Function Classes and Objects
1Function Classes and Objects
- Andy Wang
- Data Structures, Algorithms, and Generic
Programming
2Function 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)
-
3Function 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 gtgt ch
- muc(ch)
4Two roles of operator()
- Overloading an operator by name
- Obtaining input parameters of the operator
function
5Another 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 wildcardoperator() (const String S)
const - String R(S)
- for (unsigned int i 0 i lt S.Size() )
- if (Si v) Ri ?
-
- Return R
6Another Example of a Function Class (2)
- include ltiostream.hgt
- include ltxstring.hgt
- int main()
- String S(character)
- wildcard k_to_?(k)
- wildcard i_to_?(i)
-
- cout ltlt k_to_?(S) ltlt endl // ?hara?ter
- cout ltlt i_to_?(S) ltlt endl // ch?r?cter
7Advantages of Function Objects
- Abstraction
- Template parameters
- Versatility
- Dynamically create functions with different
behaviors - Performance
- More information available to compilers to
generate faster code
8More on Function Objects
- Most common use in STL Predicate Classes
- Function classes that return a Boolean value
- Examples LessThanltTgt, GreaterThanltTgt
- Other examples
- Hash objects
- Wrapper for any function to be applied by generic
algorithms (e.g., makeuppercase) - IO manipulators
- Dynamically configurable functions
9Predicate Class TLessThanltTgt
- template lttypename Tgt
- class TLessThan
- public
- int operator() (const T t1, const T t2)
const -
- template lttypename Tgt
- int TLessThanltTgtoperator() (const T t1, const
T t2) const - return (t1 lt t2)
10Predicate Class TGreaterThanltTgt
- template lttypename Tgt
- class TGreaterThan
- public
- int operator() (const T t1, const T t2)
const -
- template lttypename Tgt
- int TGreaterThanltTgtoperator() (const T t1,
const T t2) const - return (t1 gt t2)
-
11Function Class THashClassltTgt
- template lttypename Tgt
- class THashClass
- public
- unsigned int operator() (const T) const
-
- template lttypename Tgt
- unsigned int THashClassltTgtoperator() (const T
t) const - return hash_function(t)
-
12Simple I/O Manipulators
- Use
- unsigned long y
- cout ltlt hex ltlt y // output y in hex notation
- Implementation
- ios operatorltlt(ios s, ios F(ios))
- F(s)
- return(s)
-
- ios hex(ios s)
- s.unsetf(iosoct iosdec)
- s.setf(ioshex)
- return s
13I/O Manipulators with Parameters
- Use
- cout ltlt beep(10) // output 10 beeps
- Implementation (non-generic)
- class beep
- private
- int numbeeps
- public
- beep(int n) numbeeps(n)
- ostream operator() (ostream os) const
- for (int i 0 i lt numbeeps i)
- os ltlt \a
-
- return os
-
14I/O Manipulators with Parameters (2)
- ostream os operatorltlt(ostream os, beep bp)
- bp(os)
- return os
15I/O Manipulators with Parameters Using Templates
- Generic one-parameter manipulator class
- templateltclass S, typename Tgt
- 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
-
16I/O Manipulators with Parameters Using Templates
(2)
- Generic overload of left shift operator
- templateltclass S, typename Tgt
- S operatorltlt(S s, const _STFOltS, Tgt fo)
- return fo(s)
-
- Instantiation for beep(n)
- ostream _beep(ostream s, int n)
- for (int i 0 i lt n i)
- s ltlt \a
- return s
-
-
- _STFOltostream, intgt beep(int n)
- return _STFOltostream, intgt ((ostream ()
(ostream, int)) _beep, n)
17Generic Overload of Left Shift Operator
- templateltclass S, typename Tgt
- S operatorltlt(S s, const _STFOltS, Tgt fo)
- return fo(s)
-