One option would be to keep two separate environments with almost the same settings, except for the background color; inside the table you use the environment without the background color and let \columncolor
(from the colortbl
package, loaded through the table
option for xcolor
) add the color:
\documentclass{article}
\usepackage{booktabs}
\usepackage{listings}
\usepackage[table]{xcolor}
\colorlet{listingscolor}{black!15}
\lstnewenvironment{mylistings}
{\lstset{language=C++,
backgroundcolor=\color{listingscolor}, % set backgroundcolor
basicstyle=\footnotesize,% basic font setting
}%
}
{}
\lstnewenvironment{cpptable}
{\lstset{language=C++,
basicstyle=\footnotesize,% basic font setting
}%
}
{}
\begin{document}
\begin{table}
\caption{C++ DIC class functions.}
\centering
\begin{tabular}{>{\columncolor{listingscolor}}l c c c}
\toprule[0.2em]
\multicolumn{1}{c}{\textbf{Function in C++}} & \textbf{Input} & \textbf{Output} & \textbf{Document corresponding chapter}\\
\midrule
\multicolumn{4}{c}{Main setup and API functions. Public functions.}\\
\begin{cpptable}
initialize()
\end{cpptable}
&sdf & df&f \\
\begin{cpptable}
another_function()
\end{cpptable}
&sdf & df&f \\
\begin{cpptable}
yet_another_function()
\end{cpptable}
&sdf & df&f \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

Another option is to locally choose an appropriate value for linewidth
:
\documentclass{article}
\usepackage{booktabs}
\usepackage{listings}
\usepackage[table]{xcolor}
\lstset{ %
language=C++,
backgroundcolor=\color{black!15}, % set backgroundcolor
basicstyle=\footnotesize,% basic font setting
}
\begin{document}
\begin{table}
\lstset{linewidth=4.5cm}
\caption{C++ DIC class functions.}
\centering
\begin{tabular}{l c c c}
\toprule[0.2em]
\multicolumn{1}{c}{\textbf{Function in C++}} & \textbf{Input} & \textbf{Output} & \textbf{Document corresponding chapter}\\
\midrule
\multicolumn{4}{c}{Main setup and API functions. Public functions.}\\
\begin{lstlisting}
initialize()
\end{lstlisting}
&sdf & df&f \\
\begin{lstlisting}
another_function()
\end{lstlisting}
&sdf & df&f \\
\begin{lstlisting}
yet_another_function()
\end{lstlisting}
&sdf & df&f \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
