2002

Grusel++ of the month

Home
Download Source
/******************************************************************************/
/*                                                                            */
/*                                                         FILE: december.cpp */
/*                                                                            */
/*  I am still surprised by some variants of misusing operators               */
/*  ===========================================================               */
/*                                                                            */
/*  Compiled and tested with Visual C++ V6.0                                  */
/*                                                                            */
/*  V1.01   27-DEC-2002   P. Tellenbach   http://www.heimetli.ch/             */
/*                                                                            */
/******************************************************************************/

#include <iostream>

struct X
{
   char *ps ;

   X &operator *()
   {
      std::cout.put( *ps++ ) ;

      return *this ;
   }

   int operator *( const X &x )
   {
      return 0 ;
   }
} x = { "Hello world !\n" } ;

int main()
{
   return * * * * * * * x * * * * * * * * x ;
}

Update 5. December 2022

The compiler complained because a string literal was assigned to a non-const pointer. A single const fixed the problem.

/******************************************************************************/
/*                                                                            */
/*                                                         FILE: december.cpp */
/*                                                                            */
/*  I am still surprised by some variants of misusing operators               */
/*  ===========================================================               */
/*                                                                            */
/*  Compiled and tested with Visual C++ V6.0                                  */
/*                                                                            */
/*  V1.01   27-DEC-2002   P. Tellenbach   https://www.heimetli.ch/            */
/*                                                                            */
/*  Compiled and tested with g++ 11.2.0                                       */
/*                                                                            */
/*  V1.10   05-DEC-2022   P. Tellenbach   https://www.heimetli.ch/            */
/*                                                                            */
/******************************************************************************/

#include <iostream>

struct X
{
   const char *ps ;

   X &operator *()
   {
      std::cout.put( *ps++ ) ;

      return *this ;
   }

   int operator *( const X &x )
   {
      return 0 ;
   }
} x = { "Hello world !\n" } ;

int main()
{
   return * * * * * * * x * * * * * * * * x ;
}