My preamble is as follows:
\documentclass{llncs}
\usepackage{xcolor}
\usepackage{harvard}
\usepackage{listings}
\usepackage{chngcntr}
\makeatletter
\lst@InstallKeywords k{attributes}{attributestyle}\slshape{attributestyle}{}ld
\makeatother
\definecolor{bluekeywords}{rgb}{0,0,1}
\definecolor{greencomments}{rgb}{0,0.5,0}
\definecolor{redstrings}{rgb}{0.64,0.08,0.08}
\definecolor{xmlcomments}{rgb}{0.5,0.5,0.5}
\definecolor{types}{rgb}{0.17,0.57,0.68}
\definecolor{cyan}{rgb}{0.168,0.568,0.686}
%\counterwithin{lstlisting}{section}
\lstset{language=C++,
basicstyle=\ttfamily,
literate={~} {$\sim$}{1},
showspaces=false,
showtabs=false,
breaklines=true,
showstringspaces=false,
breakatwhitespace=true,
escapeinside={(*@}{@*)},
commentstyle=\color{greencomments},
keywordstyle=\color{bluekeywords}\bfseries,
stringstyle=\color{redstrings},
basicstyle=\ttfamily,
morekeywords=
{ abstract, event, new, struct,
as, explicit, null, switch,
base, extern, object, this,
bool, false, operator, throw,
break, finally, out, true,
byte, fixed, override, try,
case, float, params, typeof,
catch, for, private, uint,
char, foreach, protected, ulong,
checked, goto, public, unchecked,
class, if, readonly, unsafe,
const, implicit, ref, ushort,
continue, in, return, using,
decimal, int, sbyte, virtual,
default, interface, sealed, volatile,
delegate, internal, short, void,
do, is, sizeof, while,
double, lock, stackalloc,
else, long, static,
enum, namespace, string, var, \#region, \#endregion
},
moreattributes={Assert, Test, GeneticAlgorithmHarmonizer, ExpectedException,
InvalidElitismRate,
InvalidMutationRate,
InvalidPopulationSize,
ScoreManagementUnitTests, TestFixture, Double, Int32, MusicalStyle, KeySignature, TimeSignature}, % etc...
attributestyle = \bfseries\color{cyan}, % (for instance)
backgroundcolor=\color{black!5}, % set backgroundcolor
basicstyle=\footnotesize% basic font setting
}
I have the following listing in my document:
\begin{lstlisting}[basicstyle=\ttfamily, caption={class foo managing private memory},label={lst:baseFoo}]
class foo
{
public:
foo() { aReallyLargeArray = new char[123456789]; }
~foo() { delete [] aReallyLargeArray; }
private:
char* aReallyLargeArray;
};
int main(int argc, char** argv)
{
foo bar;
}
\end{lstlisting}\bigskip
However, indentation in the input is not preserved in the output. Note how the ~foo() destructor is indented much more than foo on the previous line. This improper indentation occurs in other listings of mine as well. I suspect it has to do with the ~... Any thoughts?

~foobeing preceded by a tab character, whereasfoo(on the previous line) is preceded instead by 4 spaces; see http://tex.stackexchange.com/questions/166585/tabs-vs-spaces-in-listings-whats-the-difference-and-should-i-care. To avoid any surprises like this one, I recommend using spaces throughout your listing. – jub0bs May 16 '14 at 12:10