namespaces/namespace-1.cc - Illustration of using declarations and using directives. | Lecture 2 - slide 41 : 46 Program 1 |
// Reproduced from page 847 of 'The C++ Programming Language'. #include <iostream> #include <string> namespace X { int i = 10, j = 11, k = 12; } int k; // Global variable void f1(){ // Illustrates using directives int i = 0; using namespace X; // A using directive makes i, j, and k from X accessible i++; // local i j++; // X::j k++; // X::k or ::k reference to k is ambiguous ::k++; // global k X::k++; // X::k } void f2(){ // Illustrates using declarations int i = 0; using X::i; // double declaration: i already declared in this scope using X::j; using X::k; // hides global k i++; // local i j++; // X::j k++; // X::k } int main(){ f1(); f2(); }