2001

Grusel++ of the month

Home
Download Source
/******************************************************************************/
/*                                                                            */
/*                                                          FILE: october.cpp */
/*                                                                            */
/*  Bringing some order into a heap of characters with overloaded operators   */
/*  =======================================================================   */
/*                                                                            */
/*  Compiled and tested with Visual C++ V6.0                                  */
/*                                                                            */
/*  V1.00   21-OCT-2001   P. Tellenbach   http://www.heimetli.ch/             */
/*                                                                            */
/******************************************************************************/

#include <iostream>

class Grusel
{
 protected:
   char *text ;
   int   end ;

 public:
   Grusel( char *txt ) : text(txt), end(strlen(txt))
   {
   }

   ~Grusel()
   {
      std::cout << std::endl ;
   }

   bool operator<( int pos )
   {
      return pos + pos + 1 < end ? (*this)[pos + pos + 1] : pos < end ;
   }
  
   bool operator>( int pos )
   {
      return (*this)[(pos << 1) + 2], pos > 0 ;
   }

   bool operator[]( int pos )
   {
      return *this < pos && std::cout.put(text[pos]).good() && *this > pos ;
   }
} ;

int main()
{
   return Grusel( "ol e l!Hlowrd" )[ 0 ] ;
}

Update 22. October 2021

Added the cstring-header and some const declarations.

/******************************************************************************/
/*                                                                            */
/*                                                          FILE: october.cpp */
/*                                                                            */
/*  Bringing some order into a heap of characters with overloaded operators   */
/*  =======================================================================   */
/*                                                                            */
/*  Compiled and tested with Visual C++ V6.0                                  */
/*                                                                            */
/*  V1.00   21-OCT-2001   P. Tellenbach   https://www.heimetli.ch/            */
/*                                                                            */
/*  Compiled and tested with g++ V10.2.1                                      */
/*                                                                            */
/*  V1.00   21-OCT-2021   P. Tellenbach   https://www.heimetli.ch/            */
/*                                                                            */
/******************************************************************************/

#include <iostream>
#include <cstring>

class Grusel
{
 protected:
   const char *text ;
   int         end ;

 public:
   Grusel( const char *txt ) : text(txt), end(strlen(txt))
   {
   }

   ~Grusel()
   {
      std::cout << std::endl ;
   }

   bool operator< int pos )
   {
      return pos + pos + 1 < end ? (*this)[pos + pos + 1] : pos < end ;
   }
  
   bool operator> int pos )
   {
      return (*this)[(pos << 1) + 2], pos > 0 ;
   }

   bool operator[]( int pos )
   {
      return *this < pos && std::cout.put(text[pos]).good() && *this > pos ;
   }
} ;

int main()
{
   return Grusel( "ol e l!Hlowrd" )[ 0 ] ;
}