String Implementation: Member Operators
String& String::operator = (const String& s)
{
if (this != &s)
{
if (size_ != s.size_)
{
Clear();
Clone(s);
}
else if (size_ != 0)
{
StrCpy (data_, s.data_);
}
}
return *this;
}
char& String::operator [] (size_t n)
// overload of the array access operator
// Note: [] returns a reference to the element, hence can be used on
// the left (receiving end) of assignment.
// Distinctions between [] and Element():
// Element(n) returns a value, equal to the element data_[n]
// when 0 <= n < size_ and to '\0' otherwise.
// Element(n) cannot be used to assign a (new) value to the element.
// [n] returns a reference to the element when 0 <= n < size_
// and exits otherwise.
// [n] can be used to assign a value to any element except the last.
{
if ((size_ == 0) || (n >= size_))
{
Error("index out of range");
}
return *(data_ + n);
}