At the most basic level, the syntax for namespaces is shown in the following example. namespace MySubsystem { class Fred { <-- 1 // ... }; class Wilma { <-- 2 // ... }; void funct() <-- 3 { // ... } // Other declarations for MySubsystem go in here }
Namespaces facilitate building large systems by partitioning names into logical groupings. Namespaces are "open" and can be added to at different places. For most situations, this incremental approach is probably a better alternative than trying to specify all of the names in one location, and this approach is used heavily in the standard library. However the incremental approach must be used carefully, because the effect is as if the compiler made a single pass and knew about only the names that were part of the namespace the last time the namespace was explicitly introduced into the source file. Namespaces can be nested if desired. Java has a concept similar to namespaces called packages. UML expresses namespaces as shown below (they are called packages in UML): |