title: C++: Pass by reference or use private instance variables
body: <p>What is the best way of doing this: I got an large collection class <code>ListCompletions(string digits, Lexicon &amp; lex)</code> (lex). I need to access it in the <code>containsPrefix(string prefix)</code>method. I got the option of passing the lexicon by reference between the methods (of some methods where I do not use it) or I can make a copy of it in <code>containsPrefix(string prefix)</code> saving it as a  private instance variable.</p>
  
  <p>My guess is to make a copy of it as a private instance variable would be by far the best option as passing it around within parameters would just further complicate the code, but also, private instance variables are harder to debug since it´s harder to know which methods are using it. But I am asking to be absolutely sure, so I don´t pick up any bad coding habbits.</p>
  
  <pre><code>#include "CellPhoneMindReading.h"
  
  void CellPhoneMindReading :: ListCompletions(string digits, Lexicon &amp; lex)
  {
      //cout &lt;&lt; lex.contains("fedora") &lt;&lt; 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 &lt;&lt; prefix &lt;&lt; endl;
          containsPrefix(prefix);
      }
      else {
          string options = DigitLetters(rest[0]);
          for (int i = 0; i &lt; 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 &lt;&lt; "Illegal digit" &lt;&lt; endl;
      }
  }
  </code></pre>