1999

Grusel++ of the month

Home
Download Source
/******************************************************************************/
/*                                                                            */
/*                                                         FILE: NOVEMBER.CPP */
/*                                                                            */
/*  Demonstrates how to use operator()                                        */
/*  ==================================                                        */
/*                                                                            */
/*  Compiled and tested with Visual C++ V6.0                                  */
/*                                                                            */
/*  V1.00   09-NOV-1999   P. Tellenbach   http://www.heimetli.ch/             */
/*  V1.01   19-NOV-1999   H. Hueni        http://www.glue.ch                  */
/*                                                                            */
/******************************************************************************/

#include <iostream.h>

class printf
{
 public:
   printf( char c )
   {
       cout << c ;
   }

   printf &operator()( char c ) 
   {
       cout << c ;
       return( *this ) ;
   }

   int operator()( char *s )
   {
       cout << s ;
       return( 0 ) ;
   }
} ;

/***********/
 int main( )
/***********/

{
  return printf( 'G' )( 'r' )( 'u' )( 's' )( 'e' )( 'l' )( "++" ) ;
}

Update 2. December 2019

I had to upgrade from iostream.h to iostream and the namespace std.

Surprisingly, the new header includes cstdio, which in turn includes stdio.h! This caused an error because printf in main was bound to the global printf function.

The solution to this problem is not elegant, but it compiles and runs with minimal changes.

/******************************************************************************/
/*                                                                            */
/*                                                         FILE: NOVEMBER.CPP */
/*                                                                            */
/*  Demonstrates how to use operator()                                        */
/*  ==================================                                        */
/*                                                                            */
/*  Compiled and tested with Visual C++ V6.0                                  */
/*                                                                            */
/*  V1.00   09-NOV-1999   P. Tellenbach   https://www.heimetli.ch             */
/*  V1.01   19-NOV-1999   H. Hueni        http://www.glue.ch                  */
/*                                                                            */
/*  Compiled and tested with g++ V8.3.0                                       */
/*                                                                            */
/*  V2.00   02-DEC-2019   P. Tellenbach   https://www.heimetli.ch             */
/*                                                                            */
/******************************************************************************/

#include <iostream>

using namespace std ;

class printf
{
 public:
   printf( char c )
   {
       cout << c ;
   }

   printf &operator()( char c ) 
   {
       cout << c ;
       return( *this ) ;
   }

   int operator()( const char *s )
   {
       cout << s ;
       return( 0 ) ;
   }
} ;

/***********/
 int main( )
/***********/

{
 class printf printf( 'G' ) ;

 return printf( 'r' )( 'u' )( 's' )( 'e' )( 'l' )( "++" ) ;
}