Reputation: 2152
I'm reading "Clean Code" which is advocating lots of small functions. If I have lots of small functions in my code in c++ then there's going to be functions that I probably don't want users to execute.
I don't want these new functions in my headers - but I want them declared.
Do I create two header files and have my source include both? myfuncs_public.h
and myfuncs_private.h
?
Upvotes: 0
Views: 412
Reputation: 967
It's strange, in modern time, we usually separate class in .h
file and .cpp
file, whether your function is private or not. You'd better declare it in your .h
file and implement it in .cpp
file.
Upvotes: 1
Reputation: 6224
If the functions are only used in the .cpp file where they are defined, don't declare them in your headers at all. If they're used in other compilation units inside your project but aren't part of your public API, declare them in a private header file. If they're part of your public API (if you have one), declare them in a public header file.
As a general rule, don't make a symbol more visible than it needs to be. Don't clutter up your public header files with functions that external modules aren't supposed to call, and don't clutter up your private headers with functions that are only needed the file where they are declared.
If you really want to be tidy, declare functions that are only used in the compilation unit where they are defined as static
(or put them in anonymous namespaces) and check what options your platform provides for preventing private functions that are used in other compilation units from being exported from your target library or executable.
Upvotes: 3