私は独自のString
クラスを実装しており、Concat
メソッドを記述する必要があります。
私はそれを働かせることができません。
私のコードは次のとおりです。
//the m_str is private member which is initialize in the c-tor
//this function is get a string and concat it with the original string
String &String::Concat(const char *string)
{
int original_str_size = length(m_str);
int other_str_size = length(string);
int needed_length = original_str_size + other_str_size + 1;
char *str_copy = m_str;
del();
m_str = new char[needed_length];
m_size = needed_length;
int index = 0;
for(; index < original_str_size; index++)
{
if(index < original_str_size)
m_str[index] = str_copy[index];
else
m_str[index] = string[index];
}
m_str[index] = 0;
return *this;
}
メソッドの問題は、次のConcat
ようなものを書いたことです。
String word3 = word1.Contact(word2);
word3
のようにするはずですword1+word2
が、実行するとプログラムが失敗しました。
私が書いたとき:
cout << word1.Contact(word2).Length();
word
...合計の長さではなく、1 の長さだけを出力しました。