Let's say I have a library code main.cpp, where are 3 classes implemented: Inner1, Inner2 and Outer. Inner1 and Inner2 are only being used in main.cpp and they're not going to be used anywhere else.

On the other hand,Outer class is intended to be used by users of my library, i.e. to be a part of API. I also don't want my users to use inner classes.

Is it a good way to solve this by having two headers - main_api.h, where Outer class will be declared and main.h, where both inner classes will be declared?

Or is there any better way to approach this problem?

2 Answers 2


A lot of it is a matter of taste, and local naming conventions. But for sure, you should split the API part out in a separate header.

A typical approach would be to have each class definition in one header file, like Inner1.h, Inner2.h, and Outer.h. API user would only ever use Outer.h (maybe not even know there are others).

  • So far I've been using 1 header for 1 .cpp file. Is it better to use separate headers for every class?
    – Eenoku
    Commented Sep 7, 2018 at 18:29
  • Depends on class size and reusability of the classes by themselves. And on Taste. Often, the rule is to have only one class in one file. Ask yourself: has this class a life on tis own, or can it only ever be used together with another?
    – Aganju
    Commented Sep 7, 2018 at 18:29
  • The classes I have in mind will be part of one library - I was thinking about one main header, which will include their API headers. Otherwise, those classes have no use alone.
    – Eenoku
    Commented Sep 7, 2018 at 18:32

It doesn't matter how you split the headers, because all of them will end up being included. The consumer of your header sees no difference. So it's probably fine to keep using a single header.

But it is understandable that you want to mark the internal classes as internal. There are two approaches, depending on your goals:

  • If you want to provide total encapsulation of your internal classes, then use the pImpl idiom (opaque pointer). The header file then only contains the public API of the outer class, but no references to the internal classes. Instead, you put the internal classes into your .cpp file – the only place where they are used. Ideally you also enclose them in an anonymous namespace to give them internal linkage. This helps avoid linker errors, and makes extra optimizations possible.

  • If this encapsulation is merely informational, then put the internal classes into a namespace detail { ... } in the header. The detail name is merely a convention, any name that clearly communicates that its contents are internal will do. This technique might be necessary if the internal classes provide templates.

  • 1
    All the headers will be read by main.cpp. All other translation units that use the facilities of main.cpp need only read main_api.h. Commented Sep 7, 2018 at 20:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.