dyna/string4.cpp

The following code example is taken from the book
Object-Oriented Programming in C++
by Nicolai M. Josuttis, Wiley, 2002
© Copyright Nicolai M. Josuttis 2002


/* operator [] for variables
 */
String::reference String::operator [] (unsigned i)
{
    // index not in permitted range?
    if (i >= len) {
        /* throw exception:
         * - new: pass string itself and invalid index
         */
        throw RangeError(*this,i);
    }

    return reference(buffer[i]);
}

/* operator [] for constants
 */
char String::operator [] (unsigned i) const
{
    // index not in permitted range?
    if (i >= len) {
        /* throw exception:
         * - new: pass string itself and invalid index
         */
        throw RangeError(*this,i);
    }

    return buffer[i];
}