Suppose I have the following files:
- main.cpp
- routine.cpp
- routine.h
Suppose further that main.cpp calls a function routine() that is defined in routine.cpp, but routine.cpp also contains functions that are only used by the routine() function. In other words, routine.cpp contains both functions that are only called from within routine.cpp and functions that are called from other source files.
Obviously main.cpp will contain #include "routine.h". But should routine.h contain prototypes of all functions that are defined in routine.cpp (style A), or should routine.h only contain prototypes of functions that are called from other source files (style B)?
I’ve always written code after style A, but recently I’ve wondered if style B makes more sense stylistically. (If style B is used, then the prototypes of the functions that are used only inside routine.cpp could be at the top of routine.cpp, or the definitions could simply precede their usage.)
Usually, the header only contains methods of the public interface (if those are free functions). You can declare the auxiliary methods used in
routine.cppin an anonymous namespace.Such functions were previously declared
static, which gave them internal linkage, but the general style now is to use anonymous namespaces.