简体   繁体   中英

Keyword using namespace in C++

是否有逻辑上的原因,即在using namespace的关键字之后,我们不能在using namespace拥有一个名为myfunction的函数, myfunction在命名空间之外没有另一个名为myfunction函数(具有相同的原型),但是我们可以将其用于变量( myvariable in命名空间和myvariable之外的myvariable )?

Of course you can have a function with the same name and signature in different namespaces -- that's part of the reason namespaces exist. The only consideration is that if you want to call it you will have to qualify its name.

namespace Foo {
    void func();
}

namespace Bar {
    void func();
}

using namespace Foo;
using namespace Bar;

func(); // does not compile -- which func()?

Foo::func(); // ok
Bar::func(); // ok

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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