使用中括号[]表示法访问字符。使用方法operator[]()来重载中括号运算符。
[]两个操作数,第一个位于中括号前面,第二个位于两个中括号中间。
//read-write char access for non-const String
char &String::operator[](int i)
{
return str[i];
}
//read-only char access for const String
const char &String::operator[](int i) const
{
return str[i];
}