TITLE: Const methods can modify a class's logical state

[ This example adapted from "C++ Gotchas", tutorial note
  copyright by Tom Cargill, p. 63 ]


#include <iostream.h>


class String {
  private:
    char* rep;
  public:
    String (const char*);
    void toUpper() const;
    ...
};


String :: String (const char* s)
{
    rep = new char [strlen(s)+1];
    strcpy (rep, s);
}


void String :: toUpper () const
{
    for (int i = 0; rep [i]; i++)
	rep[i] = toupper(rep[i]);
}


int main ()
{
    const String lower ("lower");
    lower.toUpper ();

    cout << lower << endl;
    return 0;
}


[ This program will output "LOWER". For more information,
  see the ARM p. 177. 

  Note that this is a "standard" technique for managing
  cached values where avoid "cast away const" is a concern.
  Of course, the tradeoff is the inefficiency of heap
  allocation.  -adc ]
