Should helper functions be excluded from header file declarations? (C++)

Should helper functions which are only used within the implementation file be excluded from header file declarations?

By helper function I mean a convenience function that is closely associated with a class, but doesn't represent an operation on the class, does not need access to member variables, and as such is not a method.

On one hand, including them in the header file makes the header file a comprehensive reference for the corresponding c++ implementation. On the other hand, it introduces one more piece of code to maintain for consistency. More importantly, including helper functions encourages minor violations of a layer of abstraction, in the sense that the function isn't meant to be run in other contexts, although not in a severe way since there's not a risk of corrupting class state so long as the helper function itself is not breaking encapsulation of classes it works with.

Answers


On one hand, including them in the header file makes the header file a comprehensive reference for the corresponding c++ implementation.

That's not the purpose of a header file. A header file is supposed to be a reference for the interface, not the implementation. Some implementation details will sometimes leak through, but this should be avoided wherever possible.

So to answer your headline question: yes.


If they're used in one part of the implementation only they should be excluded from the main header files. However if you wish to test those function you will want to add references to them to a separate headerfile.

If they're used in multiple parts of the implementation they'll belong in their own internal header file so that they can be shared.


Need Your Help

Using Context.RewritePath to rewrite to a different-domain

.net vb.net url-rewriting

this is my first post on SO. :) My goal is, in the application (which is an existing VB.NET app shudder), whenever a request is encountered for a resource such as: