1

As far as I can tell, you can make your interface known to others by providing your .h file. Your .cpp is the implementation. Then they can see the function names, the parameter types, the return type, maybe a description of how to use a function, and maybe what it does in the .h file.

Then I read in posts here about using namespaces to separate the interface from the implementation. What does that mean? Doesn't a namespace only let you know that a name exists in that namespace? So please provide an example, I can't find any.

2
  • If I understand you clearly, you are asking about the benefits of placing interface types in different namespaces than the one in which implementing types are. Correct?
    – Crono
    Commented Mar 25, 2014 at 20:41
  • @Greg Example: The first answer says "Since namespace can be discontiguous and spread across translation units, they can also be used to separate interface from implementation details." Commented Mar 25, 2014 at 20:45

2 Answers 2

5

You don't use namespaces to separate interface from implementation in C++. Namespaces are essentially packages in C++; you use them to group together related classes.

Rather, you use classes to hide the implementation details from the user. Classes in C++ are the same as classes in any other OOP language in that regard; they provide a public interface, while encapsulating the implementation details.

In the Stack Overflow answer that you linked, the author linked this article. In a nutshell, this article states that any helper functions and operator functions that are part of the functionality of a class should go into the same namespace as that of the class, because they form part of the public API of that namespace.

That makes sense to me. Having those functions in the same namespace organizes the classes (and the functions that support them) within the same logical unit, a logical unit that exposes a public API, but encapsulates the implementation details using ordinary object orientation principles.

Further Reading
Separating Interface and Implementation in C++
The Separation of Interface and Implementation in C++
Namespaces and the Interface Principle

6
  • So what does "... namespace ... can also be used to separate interface from implementation details" mean? Commented Mar 25, 2014 at 20:53
  • It is clarified in the article that the OP of that Stack Overflow answer linked. Note that the article never mentions the phrase "using namespaces to separate interface from implementation details," although that is a natural consequence of using namespaces as an organizing principle. Commented Mar 25, 2014 at 20:54
  • So the public functions should be in one namespace, while the implementation is kept out? Commented Mar 25, 2014 at 20:58
  • The implementation is already encapsulated within the public function. All the namespace does is group that function with those classes that use it. Commented Mar 25, 2014 at 20:59
  • 1
    That would be one good reason, yes. I think the conclusion that the Stack Overflow answer arrives at is a bit misguided; the author of the Namespaces article talks about surprising behavior in your code that can be eliminated with proper namespacing, and never discusses separation of interface from implementation via namespaces as a "thing" (other than achieving the benefit of avoiding naming conflicts). Commented Mar 25, 2014 at 21:08
1

One way to separate "interface" and "implementation" with namespaces is what Boost does: for a library X, put the public API in namespace X and implementation details in namespace X::details or similar.

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