I have a single *.h file. This file contains a single (more to come) function declaration.

Now the implementation of that file is very complex. the corresponding *.cpp contains several function definitions that are used by that publicly visible function.

I would like to keep the "main" *.cpp file of that function as clean as possible and refactor the implementation details out.

For that I use a separate *.h file that exposes the private function declarations.

How can I (and even should I?) hide the implementation details?

2 Answers 2


Trying to extract implementation details from your main cpp to separate files might be counter productive since it would require to give those details more visibility than they deserve. Of course, you could use the trick of included cpps, but this is not something that I'd recommend.

Besides the approach that Bart proposed, based on static functions, you could also consider the following alternatives:

  • In the main cpp, use an anonymous namespace and put there the functions that you do not want to be visible outside. Anonymous namespaces are only visible within the compilation unit in which they are declared. They have the advantage to hide much more than only local functions.

  • If your implementation is very big, you could consider encapsulating your function in a functor class. You could then spread different helper functions across several files, and protect implementation details with the C++ access controls, making public only the main one, and keeping all the other as private.

  • 1
    anonymous namespaces seem to be the thing I was searching for :) Thanks!
    – user315117
    Mar 21, 2022 at 13:57
  • @Raildex There's even a core guideline that recommends it. I've added the link
    – Christophe
    Mar 21, 2022 at 14:42

I would like to keep the "main" *.cpp file of that function as clean as possible and refactor the implementation details out.

As the .cpp file is all about the implementation, that sounds alike a contradiction.

To keep the implementation of a complex function as clean as possible, the best advise is

  1. To make liberal use of private/static 'helper' functions.
  2. To organize all your .cpp files in the same way, using a structure that makes it easier to find what you need.

One way to organize you .cpp file it to use a structure like this:

// <File header with copyright & licensing information>

#includes "my own header.h"
<#includes of system headers>
<#includes of project headers>

/* Helper function declarations */
< declarations of static functions >

/* Helper function implementations */

< implementation of the static functions >

/* Public function implementations */

< implementation of the public functions >

Although not strictly needed, I have used separate sections for the declarations an implementation of the helper functions, because it avoids problems if your algorithm calls for a mutual recursion between two or more of the helper functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.