次の関数は、C ++ Primer、5th Editionの211ページと214ページから作成されました。この関数は、文字列内の特定の文字の最初の出現位置を返し、その文字列内のその文字の出現回数を通知します。
string::size_type find_char(const string &s, char c, string::size_type &occurs)
{
// Compares the given character with string
// Records the first occurrence of that character
// The change in &occurs is reflected back to the original variable
}
著者は、パラメーターを渡すときに「コピーを回避するためのconst
参照」を使用し、関数が変更されないパラメーターには「参照パラメーター」を使用することを推奨しています。なぜ彼らは参照パラメータを作らなかったのですか?char c
const