C ++-最佳实践:`使用std :: cout`与`std :: cout` [英] C++ - Best Practice: `using std::cout` vs `std::cout`

查看:111
本文介绍了C ++-最佳实践:`使用std :: cout`与`std :: cout`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解,在C ++中,我们永远不要使用:

I understand that, in C++, we should never use:

using namespace std;

两种可能的选择是:

1)在文件的开头添加using std::cout;,并在需要时输入cout

1) adding using std::cout; at the beginning of the file and just type cout whenever required

2)每次我们需要使用cout

2) type std::cout every time we need to use cout

我的理解是第二种方法是最好的.但是,这在专业环境中总是遵循吗?在快节奏的环境中进行跟踪是否可行?我已经习惯了第一种选择.切换是否有优势?

My understanding is that the second method is the best. However, is this always followed in professional environments? Is it practical to follow in highly fast paced environments? I am used to the first alternative. Is it an advantage to switch?

注意:我最初将此内容发布在代码审查"中,并被告知该主题属于此处.请让我知道是否可以.

Note: I originally posted this in Code Review and I was told that this topic belonged here. Kindly let me know if not.

推荐答案

所以我对C ++做了一些工作,但是我想说的是,该问题属于所有语言的名称空间问题.真正的问题是,如果您具有具有相同功能的多个名称空间,那么将更难以读取正在发生的事情,并且可能导致不良结果.

So I have done a little bit with C++, but I would say the problem falls under the problem of namespaces in all languages. The real problem is that if you have multiple namespaces with the same function, it makes it much more difficult to read whats going on and can cause undesired results.

例如,如果您在两个名称空间中有两个具有相同名称的函数,那么代码将如何知道要使用哪个函数?当您添加了多个名称空间并调用一个函数时,还会出现另一个问题.读取代码的人如何知道代码来自哪个命名空间?在函数前面放置名称空间有助于提高代码的可读性.

For example if you have two functions with the same name in two name spaces, how will the code know which one to use? Also another problem comes when you have multiple namespaces added and call a function. How does someone reading the code know which namespace the code is coming from? Having the namespace in front of the function helps make your code more readable.

这篇关于C ++-最佳实践:`使用std :: cout`与`std :: cout`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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