-1

I have a very basic question about namespace

when should I use "using namespace A::B"?

I know I shouldn't use it in header files, how about .cpp file? In my test.cpp:

namespace A{
namespace B{
namespace C{
      A::B::Object obj = ....
}
}
}

the full namespace is A::B::Object as I have right now, Do I actually need A::B? Can I just have Object obj = ....? Should I use using namespace A::B and then have Object obj = ....? I am not sure what this actually means:

    namespace A{
    namespace B{
    namespace C{
          .....
    }
    }
    }

I know This means all the contents inside it will have namespace A::B::C, but does it also mean:

using namespace A
using namespace A::B
using namespace A::B::C

implicitly for the contents inside it?

code3
  • 81
  • 1
  • 8
  • 1
    Does this answer your question? [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Stephen Newell Sep 29 '21 at 22:35

2 Answers2

1

Because obj is not on the root (global) namespace if you write outside its namespace Object the identifier will be not found. So you have a few options:

  • use fully qualified name:

    A::B::C::Object obj = ...
    
  • use using declaration:

    using namespace A::B::C;
    Object obj = ...;
    
  • use using declaration:

    using A::B::C::Object
    Object obj = ...
    
  • use namespace alias:

    namespace X = A::B::C;
    X::Object obj = ...
    

And basically any combination of above e.g.:

namespace Y = A::B;
Y::C Object obj = ...

using namespace A::B;
C::Object obj = ...
bolov
  • 72,283
  • 15
  • 145
  • 224
1

Statements placed in the inner namespaces will be able to reference classes, typedefs, functions, etc from the outer namespaces without explicitly typing them.

namespace A {
    class AClass { };
    namespace B {
        class BClass {
            AClass a;   // Does not require A::AClass
        };
    }
}

In addition to using namespace, another way to shorten lengthy compounded namespaces in a .cpp file is to declare a new, shorter namespace:

// MyClass.h
namespace A {
    namespace B {
        class MyClass { 
        public:
            void f();               
        };
    }
}
// MyClass.cpp
#include "MyClass.h"

namespace AB = ::A::B;

void AB::MyClass::f() {
    // impl...
}

(Note that the optional :: at the start of ::A::B explictly tells the compiler that ::A::B is the full path, which is potentially useful if a header had an SomeOtherSpace::A::B namespace and carelessly wrote using namespace SomeOtherSpace.)

Jack Harwood
  • 348
  • 2
  • 7
  • thanks Jack, what if `class AClass { };` is not defined in this file but another .cpp file under same namespace `A`, is it also true that I can still use `AClass a` and no need to write `A::AClass a`? – code3 Sep 30 '21 at 01:12
  • Yes; declaring `AClass` in namespace `A` in a .h file, defining `A::AClass` in a .cpp file, and `#including` the .h in a different .cpp file will allow you to use `AClass` in that .cpp file, either with `A::`, `using namespace A`, or by writing the code within a new `namespace A { ... }` block. – Jack Harwood Sep 30 '21 at 05:03