问题描述
所以,我在规范文件中有以下内容
#include< string&
#include< fstream>
使用命名空间std:
class MyStuff
{
private:
$ b string name;
fstream file;
//其他东西
public:
void setName(string);
}
我在实现文件中也有
#includeMyStuff.h
using namespace std;
void MyStuff :: setName(string name);
{
name = name
}
文件我有...
#include< iostream>
#include< string>
using namespace std;
void main()
{
string name;
MyStuff Stuff;
cout<< 输入你的名字: ;
getline(cin,name);
Stuff.setName(name);
}
我收集应用using namespace std;在头文件中是一个no-no,并且完全限定是更好的做法;例如 std :: cout<<东西< endl;
这是我的理解,为了使用一个字符串,它必须有std命名空间。这是真的吗?
如果是这样,在头文件中,更多的是纯净/干净做为...
#include< string>
class MyStuff
{
std :: string name;
}
根据我目前的理解,使用namespace std;在所有三个文件,规范,实现和程序之间,基本上将三个命名空间层叠在一起,所以如果我在每个文件中单独声明 string name;
编译器不知道哪个去哪个。是真的吗?
我一般认为清楚是一个好的事情,但我有点不清楚如何的具体性,我最感兴趣的是更深的为什么的基础。
所以我的直接问题是,在我提供的示例,什么是最清晰的为编译器和行业标准?
假设我声明一个命名空间的原因和实际实现。 class string
自己。因为我是一个懒惰的屁股,我在全局命名空间。
// Solar的东西
class string
{
public:
string();
// ...
};过了一段时间,我意识到重新使用一些 代码将使我的项目受益。感谢您使用开源,我可以这样做: #include< solarstuff.hpp>
#include< phoenixstuff.hpp>
string foo;
但是突然编译器不再喜欢我了。因为有一个 :: string
(我的类)和另一个 :: string
标准的,包含在你的头部,并带入到使用命名空间std;
)的全局命名空间,有各种痛苦。
更糟糕的是,这个问题会通过包含我的标题(其中包括您的标题,这是你的想法)的每个文件得到提升。
是的,我知道,在这个例子中,我也怪我不在自己的命名空间保护我自己的类,但这是我想出的ad-hoc。
命名空间可以避免标识符冲突。您的标头不仅向全局命名空间引入 MyStuff
,还从 string
引入每个和 fstream
。
添加:从我们的任何一个实际上都不需要它们,所以为什么要将它们拖入全球,污染环境?维护编码器/调试器的视图, foo :: MyStuff
比 MyStuff
十倍方便,命名空间其他地方(可能甚至不是相同的源文件),因为你得到的命名空间信息在代码中的那一点,你需要它。
So, I have the following in a specification file
#include <string>
#include <fstream>
using namespace std:
class MyStuff
{
private:
string name;
fstream file;
// other stuff
public:
void setName(string);
}
I also have in the implementation file
#include "MyStuff.h"
using namespace std;
void MyStuff::setName(string name);
{
name = name
}
and in the program file I have...
#include <iostream>
#include <string>
using namespace std;
void main()
{
string name;
MyStuff Stuff;
cout << "Enter Your Name: ";
getline(cin, name);
Stuff.setName(name);
}
And I'm gathering that applying "using namespace std;" in a header file is a no-no, and that to fully qualify is the "better" practice; such as std::cout << stuff << endl;
It is my understanding that in order to use a string, it must have the std namespace. Is that true?
If so, in the header file, is more "pure/clean" to do it as...
#include <string>
class MyStuff
{
std::string name;
}
And, as I understand it currently, using namespace std; across all three files, specification, implementation, and program, essentially layers the three namespaces on top of each other, so if I separately declare string name;
within each of the files, the compiler will not know which goes to what. Is that true?
I generally understand that being clear is a "good" thing to do, I am however a little unclear on the specificity of how, and I'm most interested in the deeper "why" that underlies it all.
So my direct question is, in my example provided, what is the "clearest" way to describe the function both for the compiler and for industry "standard"? And, can you direct me to resources that more clearly delineate the reasoning and practical implementation of namespaces.
解决方案 Let's say I declare a class string
myself. Because I'm a lazy bum, I do so in global namespace.
// Solar's stuff
class string
{
public:
string();
// ...
};
Some time later on, I realize that re-using some of your code would benefit my project. Thanks to you making it Open Source, I can do so:
#include <solarstuff.hpp>
#include <phoenixstuff.hpp>
string foo;
But suddenly the compiler doesn't like me anymore. Because there is a ::string
(my class) and another ::string
(the standard one, included by your header and brought into global namespace with using namespace std;
), there's all kinds of pain to be had.
Worse, this problem gets promoted through every file that includes my header (which includes your header, which... you get the idea.)
Yes I know, in this example I am also to blame for not protecting my own classes in my own namespace, but that's the one I came up with ad-hoc.
Namespaces are there to avoid clashes of identifiers. Your header not only introduces MyStuff
into the global namespace, but also every identifier from string
and fstream
. Chances are most of them are never actually needed by either of us, so why dragging them into global, polluting the environment?
Addition: From the view of a maintenance coder / debugger, foo::MyStuff
is ten times more convenient than MyStuff
, namespace'd somewhere else (probably not even the same source file), because you get the namespace information right there at the point in the code where you need it.
这篇关于使用命名空间std;在头文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!