3.10 Namespaces In C++

Namespaces.cc


#include <string>
#include <iostream>
using namespace std;                                              //(A)

namespace Module1 {
    void foo();         // this is only a declaration,
                        // definition will come later in line (D)
}

namespace Module2 {
    void foo() { cout << "Module2 foo() invoked" << endl; }
}

namespace Module3 {
    using namespace Module1;    // has 'foo'                      //(B)
    using namespace Module2;    // also has 'foo', but            //(C)
                                // no problem at this point
    void bar() { cout << "Module3 bar() invoked" << endl; }
}

namespace Module4 {
    void foo() { cout << "Module4 foo() invoked" << endl; }
}

namespace Module5 {
    void bar() { cout << "Module5 bar() invoked" << endl; }
}

// foo of Module1 defined outside the namespace Module1. Must
// therefore use namespace-qualified name for foo:
void Module1::foo() {cout << "Module1 foo() invoked" << endl;}    //(D)

// The global foo:
void foo() { cout << "top level foo() invoked" << endl; }

//Addition to Module5:
namespace Module5 {   
    void hum() { cout << "Module5 hum() invoked" << endl; }
}


int main() {
    //This statement invokes global foo()
    foo();                                                        //(E)

    Module1::foo();                                               //(F)
    Module2::foo();                                               //(G)

    //The following statement, if uncommented, results 
    //in compiler error because Module1 and Module2 
    //both have foo()
    //  Module3::foo();                                           //(H)

    Module3::bar();                                               //(I)

    using namespace Module4;    

    //The following statement, if uncommented, results 
    //in compiler error because foo() of Module4 
    //conflicts with the global foo() 
    //  foo();                                                    //(J)
               
    //But the following statement is okay since it uses 
    //the scope operator for invoking the global foo()
    ::foo();                                                      //(K)
                            
    using namespace Module5;
    bar();                                                        //(L)
    hum();                                                        //(M)
    return 0;
}

Results


C:\classes\ece538\work\ch03>cl -EHsc -W4 Namespaces.cc
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

Namespaces.cc
Microsoft (R) Incremental Linker Version 8.00.50727.42
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:Namespaces.exe
Namespaces.obj

C:\classes\ece538\work\ch03>Namespaces
top level foo() invoked
Module1 foo() invoked
Module2 foo() invoked
Module3 bar() invoked
top level foo() invoked
Module5 bar() invoked
Module5 hum() invoked

Namespaces2.cc

namespace Module1 {
    class X {};
    class Y {};
}

namespace Module2 {
    class X {};
    class Y {};
}

int main() {
    using Module1::X;                                             //(A)
    X x1;
    // using Module2::X;        // ERROR, name conflict           //(B)
    X x2;                 
    return 0;
}

Namespaces3.cc

#include <iostream>
using namespace std;

namespace Module1 {
    class X {};
}

namespace Module2 {
    void foo(){ cout << "foo of Module2 invoked" << endl; }
    void bar(){ cout << "bar of Module2 invoked" << endl; }
}

namespace Module3 {
    using namespace Module1;
    typedef X Y;
    using Module2::foo;
    class Z {};
}

int main() 
{
    Module3::X x;
    Module3::Y y;
    Module3::foo();
    //  Module3::bar();       // ERROR.  No bar in Module3.
    return 0;
}

Namespaces4.cc

#include <iostream>
using namespace std;

namespace Module1 {
    typedef int Type;
    Type foo( Type arg ) { return arg; }
}

namespace Module2 {
    typedef double Type;
    Type foo( Type arg ) { return arg; }
}

int main() 
{
    {
        using namespace Module1;
        Type x = 100;                      // int
        cout << foo( x ) << endl;          // 100
    }
    {
        using namespace Module2;
        Type x = 3.14;                     // double
        cout << foo( x ) << endl;          // 3.14
    }
    {
        using Module1::foo;
        cout << foo( 100 ) << endl;        // 100
    }
    {
        using Module2::foo;
        cout << foo( 3.14) << endl;        // 3.14
    }
    return 0;
}

NamespaceNested.cc

#include <iostream>
#include <string>
using namespace std;

namespace N1 {
    typedef int Type;
    namespace N2 {
        typedef int* Type;
        namespace N3 {
            typedef string Type;
            namespace N4 {
                typedef string* Type;
            }
        }
    }
}

int main()
{
    using namespace N1;

    Type x = 10;                            // Type is int
    cout << x << endl;                      // 10

    N1::N2::Type p = &x;                    // Type is int*
    cout << *p << endl;                     // 10

    N1::N2::N3::Type str( "hello" );        // Type is string
    cout << str << endl;                    // "hello"

    N1::N2::N3::N4::Type q = &str;          // Type is string*
    cout << *q << endl;                     // "hello"

    namespace N_FOUR = N1::N2::N3::N4;      // namespace alias    //(A)
    N_FOUR::Type ptr = &str;              
    cout << *ptr << endl;                   // "hello"

    return 0;
}

Koenig.cc

#include <iostream>
using namespace std;                                           

namespace Module1 {                                               //(A)
    class X {};                                                   //(B)
    void foo( X xobj ) { cout << "Module1's foo(X) invoked"; }    //(C)
}

namespace Module2 {
    void foo( Module1::X xobj ) { cout << "X's foo(X) invoked"; } //(D)
}

void foo( int i ) { cout << "global foo(int) invoked"; }          //(E)

int main() {
    Module1::X xob;                                               //(F)
    foo( 1 );                   // global foo(int) invoked        //(G)
    foo( xob );                 // Module1's foo(X) invoked       //(H)
    return 0;
}


Maintained by John Loomis, last updated 30 Dec 2006