The Str
class overloads some operators but not others. I will be the first to
agree that the reasons for why something should or should not be
overloadable are controversial. In any case, I present my reasons
below.
operator=
for for Str
and
char*
instead of defining copy()
. This is to keep
syntax consistent between the copy constructor used by C++ (which Str
implements) and copying to an object that already exists.
Another reason is that C++'s default "bit-copy" implementation for =
is
not a good idea with Str
because it would create two copies of
size_and_flags
that point to the same data
.
getChar()
and
setChar()
over operator[]
over because the compiler can
get confused about the []
syntax when types other than
int
are used as the index type.
equals()
in place of operator==
for
string content equality because of C/C++'s strong roots in using ==
for
pointer operations. Using ==
for something that does not
execute in constant time just does not sit well with me. Another
reason is that ==
is usually grouped as a set with !=
,
>
, <
, >=
and <=
and I did not want to
provide all of these operators for the same core reason.
append()
in place of operator+=
because I prefer to keep +=
in the numerical domain. Also
+=
is often grouped as a set with ++
, --
, and
-=
and I didn't not want to try and guess what these operators
should do to a string.
operator>>
and operator<<
. To counteract this, I
provided a simple #define
that can remove the code from the
compiled output in cases where C++ streams are not used.