简体   繁体   English

C ++命名空间和头文件

[英]C++ Namespaces and header files

I have seen codes with using namespace std; 我见过using namespace std;代码using namespace std; . Does it mean that if we use this then we don't have to include header files in the code or if we don't use namespaces, does it mean that we have to use std:: before every function, class? 这是否意味着如果我们使用它,那么我们不必在代码中包含头文件,或者如果我们不使用命名空间,是否意味着我们必须在每个函数,类之前使用std::

You have to include header files and use namespaces. 您必须包含头文件并使用命名空间。

The namespaces are contained in the header files, io streams like cin , cout are contained in the namespaces .So, only if you include the header file, namespace can be used. 名称空间包含在头文件中,像cincout这样的流包含在namespaces 。因此,只有包含头文件时,才能使用名称空间。 Without using namespace std , You have to use scope resolution operator every time you use those functions. using namespace std ,每次使用这些函数时都必须使用范围解析运算符。

using namespace std; means that all names from std namespace can be used without specifying their namespace explicitly (with std:: prefix). 表示可以使用std命名空间中的所有名称而不显式指定其名称空间(使用std::前缀)。 That is, after using namespace std; 也就是说,在using namespace std; , both string and std::string are valid. stringstd::string都有效。 Without using namespace std; using namespace std; , only std::string would work. ,只有std::string才有效。

Header files still must be included. 仍然必须包含头文件。

Note that usage of using namespace is often discouraged as it populates your code with all names from that namespace, and conflicts may occur. 请注意,通常不鼓励using namespace因为它使用该命名空间中的所有名称填充代码,并且可能发生冲突。

 using namespace std;

Is not really an ideal practice I would apply in a professional code base. 这不是一个理想的做法,我会在专业的代码库中应用。 The reason is that it practically "opens up" std namespace (packages in Java if you like) where you are probably doing "Hello world"ish programming ie not so severe as RT Embedded, Mission Critical, or Safety Critical. 原因是它实际上“打开”了std命名空间(如果你愿意,可以用Java编写),你可能正在做“Hello world”ish编程,即不像RT Embedded,Mission Critical或Safety Critical那么严重。 For example, I work in Interservice/Industry Training and Simulation where things are often safety/mission critical; 例如,我在Interservice / Industry Training and Simulation工作,其中通常是安全/关键任务; people will propbably have a quiet word with me if I was using multiple namespaces so openly. 如果我公开使用多个命名空间,那么人们可能会对我说一句话。 It's not really about the size of your program, it is more about Good practice . 这不是关于程序的大小,而是关于良好实践 Yes, if you have so many things to use from std namespace, then probably you can simply use it. 是的,如果您从std命名空间中使用了很多东西,那么您可以简单地使用它。 A compromise, and also what I sometimes do, is: 妥协,也是我有时做的,是:

using std::endl;
using std::string;
using std::cout;
using std::cin;
// And something like that

This "exposes" the ones that you will need for this scope and still lets you use: 这会“公开”您对此范围所需的内容,并且仍允许您使用:

string myStr;
cout << "Some cout" << endl;

Like what you mentioned in your question. 就像你在问题中提到的那样。 Why not try that? 为什么不试试呢?

A "Good bit" of all is that if you follow the approach I mentioned, it also "Upgrade" your level of knowlege in C++ namespaces and possible STLs. “好一点”的是,如果你按照我提到的方法,它还“升级”你在C ++名称空间和可能的STL中的知识水平。

I know some people will say "Well that is stil hard work" but to me it is a good compromise, up to a point . 我知道有些人会说“嗯,这是一项艰苦的工作”,但对我而言,这是一个很好的妥协, 在某种程度上 :) :)

DON'T FORGET TO ADD THE NECESSARY HEADER FILES PLEASE :-) 不要忘记添加必要的标题文件请:-)

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

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