Previous |Next |

 

3.2    Subscript Operator for String


        char & String::operator [ ] ( unsigned int Index ) const
        {
              if( Index > strlen( Buffer ) )
                cerr << "Warning: out of bounds string access" << endl;
              return Buffer[ Index ];
        }

        The subscript operator allows indexing into a String index.
This particular implementation simply issues a warning if the index is not in bounds.

        QUESTION: What other possibilities are there when the index is out of bounds?

        ANSWER:        ?
 

        main()
        {
          String a("abc");
          char c;

          c = a[1]; // assign 'b' to c
          a[1] = 'z'; // a becomes "azc"

        }
 
Previous |Next |