简体   繁体   English

c++ 中的命名空间问题如何使用“使用命名空间”

[英]namespace question in c++ how to use "using namespace"

I have a very basic question about namespace我有一个关于命名空间的非常基本的问题

when should I use " using namespace A::B "?什么时候应该使用“ using namespace A::B ”?

I know I shouldn't use it in header files, how about.cpp file?我知道我不应该在 header 文件中使用它,.cpp 文件怎么样? In my test.cpp :在我的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 ?完整的命名空间是A::B::Object就像我现在拥有的那样,我真的需要A::B吗? Can I just have Object obj =.... ?我可以只拥有Object obj =....吗? Should I use using namespace A::B and then have Object obj =.... ?我应该使用using namespace A::B然后使用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:我知道这意味着其中的所有内容都将具有命名空间A::B::C ,但这是否也意味着:

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

implicitly for the contents inside it?隐式为里面的内容呢?

Because obj is not on the root (global) namespace if you write outside its namespace Object the identifier will be not found.因为obj不在根(全局)命名空间中,所以如果您在其命名空间Object之外写入,将找不到标识符。 So you have a few options:所以你有几个选择:

  • use fully qualified name:使用完全限定名称:

     A::B::C::Object obj =...
  • use using declaration:使用 using 声明:

     using namespace A::B::C; Object obj =...;
  • use using declaration:使用 using 声明:

     using A::B::C::Object Object obj =...
  • use namespace alias:使用命名空间别名:

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

And basically any combination of above eg:基本上是以上的任意组合,例如:

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

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

Statements placed in the inner namespaces will be able to reference classes, typedefs, functions, etc from the outer namespaces without explicitly typing them.放置在内部命名空间中的语句将能够从外部命名空间引用类、typedef、函数等,而无需显式键入它们。

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:除了using namespace之外,另一种缩短 .cpp 文件中冗长的复合命名空间的方法是声明一个新的、更短的命名空间:

// 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 .) (请注意, ::A::B开头的可选::明确告诉编译器::A::B是完整路径,如果 header 具有SomeOtherSpace::A::B命名空间,这可能很有用并且不小心写了using namespace SomeOtherSpace 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM