Separate Compilation and Namespaces
Separate Compilation and Namespaces
Separate
Compilation
and Namespaces
Learning Objectives
Separate Compilation
Encapsulation reviewed
Header and implementation files
Namespaces
using directives
Qualifying names
Unnamed namespaces
Hiding helping functions
Nested namespaces
Class definitions
Separate from "using" programs
Build library of classes
Re-used by many different programs
Just like predefined libraries
Implementation File
Contains member function definitions
Separate compilation unit
Related functions
Prototypes header file
Definitions implementation file
Differences:
using declaration
Makes ONE name in namespace available
Introduces names so no other uses of name
are allowed
using directive
Makes ALL names in namespace available
Only "potentially" introduces names
NS1::fun1();
Specifies that fun() comes from namespace
NS1
Especially useful for parameters:
int getInput(std::istream inputStream);
Parameter found in istream’s std namespace
Eliminates need for using directive or declaration
Not same
Global namespace:
No namespace grouping at all
Global scope
Unnamed namespace:
Has namespace grouping, just no name
Local scope
Global namespace
Items not in a namespace grouping at all
Global scope