< previous page page_324 next page >

Page 324
LISTING 20.3 continued
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
88:    // then copies string and size
89:    String& String::operator=(const String & rhs)
90:    {
91:       if (this == &rhs)
92:          return *this;
93:       delete [] itsString;
94:       itsLen=rhs.GetLen();
95:       itsString = new char[itsLen+1];
96:       int i;
97:       for (i = 0; i<itsLen;i++)
98:          itsString[i] = rhs[i];
99:       itsString[itsLen] = \0;
100:       return *this;
101:      // cout << \tString operator=\n;
102:   }
103:
104:   //non constant offset operator, returns
105:   // reference to character so it can be
106:   // changed!
107:   char & String::operator[] (int offset)
108:   {
109:      if (offset > itsLen)
110:         return itsString[itsLen-1];
111:      else
112:         return itsString[offset];
113:   }
114:
115:   // constant offset operator for use
116:   // on const objects (see copy constructor!)
117:   char String::operator[](int offset) const
118:   {
119:      if (offset > itsLen)
120:         return itsString[itsLen-1];
121:      else
122:         return itsString[offset];
123:   }
124:
125:   // creates a new string by adding current
126:   // string to rhs
127:   String String::operator+(const String& rhs)
128:   {
129:    int totalLen = itsLen + rhs.GetLen();
130:     int i, j;
131:    String temp(totalLen);
132:    for (i = 0; i<itsLen; i++)
133:       temp[i] = itsString[i];
134:    for (j = 0; j<rhs.GetLen(); j++, i++)
135:       temp[i] = rhs[j];
136:    temp[totalLen]=\0;
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
continues

 
< previous page page_324 next page >

If you like this book, buy it!