Home > Articles > Programming > C/C++

Namespaces

  • Print
  • + Share This
Namespaces are a capable and yet intricate feature that was added to C++ several years ago. Today every non trivial C++ application uses this feature either directly or indirectly (for example, by referring to functions and classes defined in the Standard Library). This article explains the rationale behind namespaces, their usage, and interaction with other C++ constructs.
From the author of

To help you understand why namespaces were added to C++ in the first place, I'll use an analogy. Imagine that the file system on your computer did not have directories and subdirectories. All files would be stored in a flat repository and would therefore always be visible to every user and application. Consequently, extreme difficulties would arise: Filenames would clash (with some systems that limit a filename to 8 characters plus 3 for the extension, this is even more likely), and simple actions like listing, copying, or searching files would be much more difficult. Even worse, security and authorization restrictions would be severely compromised.

C++ namespaces are equivalent to directories. They enable you to hide declarations, they can be nested easily, they protect your code from name conflicts, and they do not incur any runtime or memory overhead.

 

  • + Share This
  • 🔖 Save To Your Account