Reputation: 8737
I've read some answers to other similar questions on SO that seemed to indicate that it was possible to hide a namespace away (for the purpose of hiding implementation details), but they didn't say how.
So for example, if I wanted a namespace A
which contained a namespace B
, and B
had some nasty implementation details for features in A
that I wanted hidden from the end user, is there some sort of linker trickery that I can use so that B
is not visible outside of A
, but A
is still visible to the rest of the program?
Upvotes: 0
Views: 3887
Reputation: 10557
The purpose of namespaces is to avoid the name clashes. That is essentially all. They do not provide any features to hide anything. Although for certain extend they force to write namesp_name::sometinng
. This may make the user think if this is worth doing or not.
What the comments above suggest, is basically splitting your header into public header and private header. Private header is included only in your cpp file and it is not expected to be included in the upper layer code. This works in many cases and this is helpful but it does not solve all problems.
Upvotes: 1
Reputation: 7010
What you seem to be talking about is a "Pimpl" pattern, and/or Handle/Body idiom. Or a few other names you can come up with. A really short example of such is on the boost Smart Pointer page. It explains how you can declare a class with an incomplete type into a smart pointer, and then define the implementation somewhere else. This "hides" it from an API user, in that the implementation for the "inner" class can change without re-compilation by anybody but the library creator, because the size is only in implementations, not in header files. Only the size of the "public" class is known, and remains constant. Read through the two .cpp files and the one .hpp file and see if it makes sense as to why it works.
I hope that's related to what you're trying to do.
Upvotes: 1