Sunday, October 31, 2010

Namespaces

The following code does not compile:

  namespace A
  {
    static const int a = 1;
    namespace B
   {
      static const int b = 2;
      namespace C 
      {
        static const int c = 3;
      }
    }
  }
  int main() {
    using namespace A::B;
    return B::C::c;
  }


The using statement brings namespace B into scope. If you need to get to the 'c' symbol, you need to do either absolute addressing: A::B::C::c or relative addressing C::c. It is almost analogous to changing directories in Unix.

No comments:

Post a Comment