Contents  1-5  6-11  12-16  17-21  22-27   28-33  34 - 38  39-46  Projects   MFC

   Contact
   C
   C++
   Visual Basic
   Java
   JavaScript
   DHTML
   Style Sheets
   About
   Normalization
   Active X
   TDC Binding
   PHP
   Perl and CGI
   Flash
   XML
   SQL
   Chat
   MCSE
   Linux
   Cabling   
 

   
 
    
    

A function object is an instance of a class that defines the overloaded operator ().  In this way, it can be used to call a
function.  In the example below, it is used with a template function to display output for whatever is passed to it by
reference.

#include <iostream>

using namespace std;
template<class T>

class PrintMe    {
public:
    void operator()(const T & t)
    {
	 cout << t << " ";
    }
};
int main()
{

    //We'll create a function that works like the old C printf() fucntion 
    PrintMe<int> PrintThis;
    for(int i = 0; i < 5; ++i)
	PrintThis(i);

    return 0;
}
Output:   0 1 2 3 4

 


©2004 C. Germany