这样做的最佳方法是什么:我有一个大型集合类ListCompletions(string digits, Lexicon & lex)
(lex)。我需要在containsPrefix(string prefix)
方法中访问它。我可以选择在方法之间通过引用传递词典(在某些我不使用它的方法中),或者我可以复制它并将其containsPrefix(string prefix)
保存为私有实例变量。
我的猜测是将其复制为私有实例变量将是迄今为止最好的选择,因为在参数中传递它只会使代码更加复杂,而且私有实例变量更难调试,因为它更难知道哪些方法正在使用它。但我要求绝对确定,所以我不会养成任何不好的编码习惯。
#include "CellPhoneMindReading.h"
void CellPhoneMindReading :: ListCompletions(string digits, Lexicon & lex)
{
//cout << lex.contains("fedora") << endl;
RecursiveMnemonics("", "72");
}
/*
* Function: containsPrefix
* Usage: containsPrefix(prefix);
* ----------------------------------------
* This function returns the given prefix passed as argument if it
* is found in the Lexicon database. prefixes that are not found
* is discarded and the return value is a empty string.
*/
string CellPhoneMindReading :: containsPrefix(string prefix)
{
if (
return "";
}
/*
* Function: RecursiveMnemonics
* Usage: RecursiveMnemonics(prefix, rest);
* ----------------------------------------
* This function does all of the real work for ListMnemonics and
* implements a more general problem with a recursive solution
* that is easier to see. The call to RecursiveMnemonics generates
* all mnemonics for the digits in the string rest prefixed by the
* mnemonic string in prefix. As the recursion proceeds, the rest
* string gets shorter and the prefix string gets longer.
*/
void CellPhoneMindReading :: RecursiveMnemonics(string prefix, string rest)
{
if (rest.length() == 0)
{
cout << prefix << endl;
containsPrefix(prefix);
}
else {
string options = DigitLetters(rest[0]);
for (int i = 0; i < options.length(); i++)
{
RecursiveMnemonics(prefix + options[i], rest.substr(1));
}
}
}
/*
* Function: DigitLetters
* Usage: digits = DigitLetters(ch);
* ---------------------------------
* This function returns a string consisting of the legal
* substitutions for a given digit character. Note that 0 and
* 1 are handled just by leaving that digit in its position.
*/
string CellPhoneMindReading :: DigitLetters(char ch)
{
switch (ch) {
case '0': return ("0");
case '1': return ("1");
case '2': return ("ABC");
case '3': return ("DEF");
case '4': return ("GHI");
case '5': return ("JKL");
case '6': return ("MNO");
case '7': return ("PRS");
case '8': return ("TUV");
case '9': return ("WXY");
default: cout << "Illegal digit" << endl;
}
}