title: Accessing string class with operator []
body: <p>For training, I'm trying to write a class like std::string.
  I have read that the method at(int) returns the reference to a character and throws an exception in the case the index is wrong (too long, or negative).
  The operator[] instead doesn't throw any exception.
  But since it returns a reference, if the index is too long, what does it return?
  This is the class I have:</p>
  
  <pre><code>class string
  {
      public:
      static const unsigned int length_max=100;
      string(const char* field=NULL)
      {
          if(field!=NULL)
          {
              const unsigned int length=strlen(field);
              this-&gt;field=new char[length+1];
              this-&gt;length=length;
              for(unsigned int i=0;i&lt;=length;i++)
                  this-&gt;field[i]=field[i];
          }
          else
          {
              this-&gt;field=NULL;
              length=0;
          }
      }
      string&amp; operator=(const char* field)
      {
          const unsigned int length=strlen(field);
          if(this-&gt;field!=NULL)
              delete this-&gt;field;
          this-&gt;field=new char[length];
          this-&gt;length=length;
          for(unsigned int i=0;i&lt;length;i++)
              this-&gt;field[i]=field[i];
          return *this;
      }
      string&amp; operator= (const string&amp; str)
      {
          *this=str.field;
          return *this;
      }
      operator char* ()
      {
          return field;
      }
      friend std::ostream&amp; operator&lt;&lt; (std::ostream&amp; out, string&amp; str);
      friend std::istream&amp; operator&gt;&gt; (std::istream&amp; in, string&amp; str);
      private:
      char* field;
      unsigned int length;
  };
  
  std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, string&amp; str)
  {
      out &lt;&lt; str.field;
      return out;
  }
  
  std::istream&amp; operator&gt;&gt; (std::istream&amp; in, string&amp; str)
  {
      char temp[string::length_max];
      in &gt;&gt; temp;
      str=temp;
      return in;
  }
  </code></pre>
  
  <p>I want to make the operator[], I also could make it throw an exception, but the problem is that I don't know what shall it return in the case the index is too long.
  For example the operator[] overloading is this:</p>
  
  <pre><code>    char&amp; operator[] (int i)
      {
          try
          {
              if(i&lt;0 || i&gt;=length)
                  throw indexException();
              return field[i];
          }
          catch(indexException&amp; e)
          {
              std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
          }
      }
  </code></pre>
  
  <p>This is the exception:</p>
  
  <pre><code>class indexException:std::exception
  {
      public:
      virtual const char* what()
      {
          return "Index is either too long, or negative";
      }
  };
  </code></pre>
  
  <p>It has to return a char reference and not only a char because I want to make it assignable, so for example if I have a string str I could say str[5]='c'.
  I also want to handle the exception, and not to quit the program with exit().
  So if there is an exception it doesn't return any value, the string could also be empty.
  What should I return? I would make it similar to std::string but I haven't understood very well how the one of std::string is implemented.</p>