在 C++ 中 using namespace std;
有什么用,什么时候用什么时候可以不用,
#include<iostream.h>
using namespace std;
class point
{
public:
int x;
int y;
void print()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
};
void main()
{
point pt;
pt.x=0;
pt.y=1;
pt.print();
}
这段代码如果不注释掉 using namespace std;
运行就会提示出错,为什么?
What’s included in “There’s………………” inside, the rest of it won’t be displayed on my top
Theoretically, it can be unnecessary. But the premise is that you have to write the full name, such as std::cout.
namespace
To put it simply, it is like this, (take a made-up example)Once upon a time, someone wrote a file operation module, which contained a
open(char*)
to open an ordinary file;later He also wrote a database operation module, which also had a
open(char*)
used to open a database file;one day he found that one of his programs used these two modules at the same time. As a result, the compilation failed because the functions had the same name~
The solution is very simple, just change the name~
But it is too troublesome to change so many
open()
in the two modules tofile_open
anddatabase_open
respectively, so the role ofnamespace
It shows, he just needs to simply modify the two modules tonamespace file{文件操作模块的原始内容};
andnamespace database{数据库操作模块的原始内容};
~ (just add a few lines of code)so that when he needs to open the file, he writes
file::open
, and when he needs to open it, If you writedatabase::open
in the database, there will be no duplicate name problem. But in this case, there will be no
open
function. All functions involving file operations must be prefixed withfile::
, and database operations requiredatabase::
Prefix, he suddenly found that the code he wrote before could not be compiled~Does the previous code need to be significantly changed? No, taking file operations as an example, as long as you add a sentence
using namespace file;
, thefile
namespace will be applied globally, and the compiler will try to find thefile::open
function, so the previous code can be compiled by just adding one more sentence~Back to the point, because C++ libraries use common function names such as
sort
andswap
, which can easily have the same name as user-defined functions. In order to avoid name conflicts, all library functions are placed by default. In thestd
namespace, if we also write a function calledsort
, there will be no naming conflict, because the full name of the thing in the library isstd::sort
But we are usually very cautious when writing programs, and the naming is standardized enough. Duplication of names rarely occurs. On the contrary, the prefix of
std::
is too redundant, and we have to usestd::cout
even to write output, so for the sake of convenience, here is One sentenceusing namespace std;
to omit all prefixes. But this is equivalent to blocking the functions provided bynamespace
. At this time, if you write aswap
function and the result is a naming conflict, don’t blame C++ for not providing a solution;Of course, the previous description is inappropriate. Not only functions can be placed in
namespace
, variables, classes, structures, etc. are all allowed.cout
is an examplemeans to use the standard namespace. If not, you need to use std::cout to specify the namespace
First of all, we need to understand the role of namespace; namespace is used to avoid naming conflicts;
For example, I defined a cout class myself for output;
There is such a section in the program;
There is no problem with compilation. The compiler finds my cout class and uses it for linking;
If I add the following statement, the system’s cout class is included and visible in the global scope:
At this time, the compiler is confused. Now there are two cout classes. It is unclear which one to use, so it can only report an error;
In order to avoid system errors, we can decide which class to use; there are the following types Solution:
Remove using namespace std
This method will use my custom cout, and can also use other classes in the std space (you need to add the std:: prefix when using it, such as std::string)
Add using logo
If you want to use system cout, add std::
Add a namespace to my self-defined cout (recommended practice)