手順は次のとおりです。
MyStringオブジェクトは、次の演算子をオーバーロードする必要があります。
1)前の割り当てのSet関数とGet関数を置き換えるには、括弧演算子をオーバーロードする必要があります。文字列配列の境界に違反すると、両方のインスタンスがexit(1)を発行する必要があることに注意してください。
関数が定義されている私の.cppファイル:
// My original set function to replace the character at the index passed in with the character passed in
void MyString::Set(int index, char b)
{
if(String[index] == '\0')
{
exit(1);
}
else
{
String[index] = b;
}
}
//original get function to get the character of the index passed in as argument
char MyString::Get(int i)
{
if( String[i] == '\0')
{
exit(1);
}
else
{
return String[i];
}
}
これをオーバーロードされた()演算子関数に変換するにはどうすればよいですか?私が得たものの中で最も多いものは次のとおりです。
MyString& MyString::operator()(const int index, const char b)
{
if(String[index] == '\0')
{
exit(1);
}
else
{
String[index] = b;
}
}
char& MyString::operator()(const int i)
{
if( String[i] == '\0')
{
exit(1);
}
else
{
return String[i];
}
}
私は何が間違っているのですか?