Defining namespaces

Lines 9–24 use the keyword namespace to define namespace Example. The body of a namespace is delimited by braces ({}). The namespace Example’s members consist of two constants (PI and E in lines 12–13), an int (integer1 in line 14), a function (printValues in line 16) and a nested namespace (Inner in lines 19–23). Notice that member integer1 has the same name as global variable integer1 (line 6). Variables that have the same name must have different scopes—otherwise compilation errors occur. A namespace can contain constants, data, classes, nested namespaces, functions, etc. Definitions of namespaces must occupy the global scope or be nested within other namespaces. Unlike classes, different namespace members can be defined ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.