Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
    
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 Mar 25 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." –  user2738698 Mar 25 at 20:45
add comment

1 Answer 1

up vote 4 down vote accepted

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

share|improve this answer
    
So what does "... namespace ... can also be used to separate interface from implementation details" mean? –  user2738698 Mar 25 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. –  Robert Harvey Mar 25 at 20:54
    
So the public functions should be in one namespace, while the implementation is kept out? –  user2738698 Mar 25 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. –  Robert Harvey Mar 25 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). –  Robert Harvey Mar 25 at 21:08
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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