为什么是“using namespace std”认为坏做法? [英] Why is "using namespace std" considered bad practice?

查看:228
本文介绍了为什么是“using namespace std”认为坏做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在其他人多次告诉我的老师的建议行使使用命名空间std 在代码是错误的。因此,我们应该使用 std :: cout std :: cin

I've been told by others on numerous occasions that my teacher's advice of exercising using namespace std in code was wrong. Hence, we should use std::cout and std::cin.

为什么使用命名空间std 被视为不良做法?是真的那么低效或风险声明不明确的变量(变量与 std 命名空间中的函数共享相同的名称)?或者,这是否会影响性能?

Why is using namespace std considered bad practice? Is it really that inefficient or risk declaring ambiguous variables (variables that share the same name as a function in std namespace)? Or, does this impact performance?

推荐答案

这与性能无关。但是考虑这样:你使用两个名为Foo和Bar的库:

This is not related to performance at all. But consider this: you are using two libraries called Foo and Bar:

using namespace foo;
using namespace bar;

一切正常,你可以调用 Blah c>从Foo和 Quux()从Bar没有问题。但有一天你升级到新版本的Foo 2.0,现在提供了一个名为 Quux()的函数。现在你有一个冲突:Foo 2.0和Bar导入 Quux()到你的全局命名空间。

Everything works fine, you can call Blah() from Foo and Quux() from Bar without problems. But one day you upgrade to a new version of Foo 2.0, which now offers a function called Quux(). Now you've got a conflict: Both Foo 2.0 and Bar import Quux() into your global namespace. This is going to take some effort to fix, especially if the function parameters happen to match.

如果你使用了 foo :: Blah()函数, 和 bar :: Quux(),然后介绍 foo :: Quux >将是一个非事件。

If you had used foo::Blah() and bar::Quux(), then the introduction of foo::Quux() would have been a non-event.

这篇关于为什么是“using namespace std”认为坏做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆