hurriedly% 2009-09-21 03:08 采纳率: 100%
浏览 897
已采纳

为什么"使用名称空间 std"被认为是不好的做法?

I've been told by others that writing using namespace std in code is wrong, and that I should use std::cout and std::cin directly instead.

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

转载于:https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

  • 写回答

30条回答 默认 最新

  • lrony* 2009-09-21 03:13
    关注

    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;
    

    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.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(29条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向