C++:通过引用传递或使用私有(private)实例变量

标签 c++ recursion parameter-passing pass-by-reference instance-variables

<分区>

这样做的最佳方法是什么:我有一个大型集合类 ListCompletions(string digits, Lexicon & lex) (lex)。我需要在 containsPrefix(string prefix) 方法中访问它。我可以选择在方法之间通过引用传递词典(在我不使用它的某些方法中),或者我可以在 containsPrefix(string prefix) 中复制它,将其保存为私有(private)实例变量。

我的猜测是复制它作为私有(private)实例变量是迄今为止最好的选择,因为在参数中传递它只会使代码更加复杂,而且私有(private)实例变量更难调试,因为它更难知道哪些方法在使用它。但我要求绝对确定,所以我不会发现任何不良的编码习惯。

#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;
    }
}

最佳答案

这里有两条评论。

  1. 确保 Lexicon 引用是常量。我认为其他任何事情都会显得可疑。
  2. 您的直觉是正确的 - 最好将 Lexicon 作为参数传递,而不是将其存储在私有(private)成员中。但是,如果传递的所有参数太多,成员实例可以是一个选项。只有您可以在那里选择最佳权衡。

奖金评论:为什么要使 DigitLetters 成为成员函数?它不引用任何成员数据 - 因此,作为自由函数会更好。

关于C++:通过引用传递或使用私有(private)实例变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13850654/

相关文章:

java - 当计数器从 1 开始时,递归方法出现堆栈溢出

python - 尝试在Python中使用DFS递归找到图中的所有路径

html - 使用 html 和 ngHref 进行 Angular 翻译

mysql - 从 Zend Framework 中的 2 个或更多相关表中获取值

javascript - 如何使用 jQuery 更改深层嵌套元素的 id

c++ - 初始化为一个值在性能上与赋值相比如何?

c++ - 模板化部分应用调用问题

c++ - MPI 中的发送和接收数组

c++ - 使用 const 原始引用传递参数且仅原始

c++ - Xcode 5.1.1 中关于 AllJoyn 的构建错误(找不到 alljoyn/Version.h)