0

这样做的最佳方法是什么:我有一个大型集合类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;
    }
}
4

2 回答 2

2

这里有两条评论。

  1. 确保 Lexicon 引用为 const。任何其他我认为似乎可疑的事情。
  2. 您的直觉是正确的 - 最好将 Lexicon 作为参数传递,而不是使用将其存储在私有成员中。但是,如果传递的所有参数过多,则可以选择成员实例。只有您可以在那里选择最佳权衡。

额外评论:为什么要创建 DigitLetters 成员函数?它不引用任何成员数据 - 因此,作为免费功能会更好。

于 2012-12-13T01:28:28.170 回答
2

如果您只是存储传递给类的方法的参数以在方法调用期间访问它,我会说这是代码异味,即表明某些事情已关闭。

类上的成员变量定义了它的状态,在这种情况下,词典似乎不属于类的状态,因为它只是在单个函数调用期间使用(从外部角度来看),而不是由类使用然后。

因此,在您提供的 2 个选项中,我显然更喜欢传递论点。

第三种选择是将引用添加为构造函数参数。

第四种选择是创建一个包含“RecursiveMnemonics”、“DigitLetters”和“containsPrefix”的新类,并让该新类将对 Lexicon 的引用作为构造函数参数。然后由“ListCompletions”在堆栈上创建新类。

于 2012-12-13T01:03:32.807 回答