CIS-255 Home http://www.c-jump.com/bcc/c255c/c255syllabus.htm

Namespaces


  1. Useful idea: Namespaces
  2. Namespace syntax
  3. Namespace usage
  4. Namespace directives and declarations
  5. The std namespace

1. Useful idea: Namespaces



2. Namespace syntax


  • 
    // mylibrary.h
    namespace mylibrary {
    
        class IntStack {
        public:
            void push( int value );
            //...
        };
    
        class Rational {
        public:
            Rational& operator=( Rational const& other );
            //...
        };
    
    } // No closing namespace semicolon
    
    
  • Namespaces can be re-opened:

    
    // IntStack.cpp
    namespace mylibrary {
        void IntStack::push( int value )
        {
            // ...
        }
    }
    
    
    
    // Rational.cpp
    mylibrary::Rational rat;
    
    namespace mylibrary {
        Rational& Rational::operator=( Rational const& other )
        {
            //...
        }
    }
    
    

3. Namespace usage



4. Namespace directives and declarations



5. The std namespace