使用命名空间标准;在头文件中

using namespace std; in a header file(使用命名空间标准;在头文件中)
本文介绍了使用命名空间标准;在头文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

所以,我在规范文件中有以下内容

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);
}

我在实现文件中也有

#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);
}

我正在收集应用使用命名空间标准;"在头文件中是一个禁忌,完全合格是更好"的做法;例如 std::cout <<东西<

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;

我的理解是,为了使用字符串,它必须具有 std 命名空间.是真的吗?

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;
}

而且,据我目前的理解,使用命名空间 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.

推荐答案

假设我自己声明了一个 string 类.因为我是一个懒惰的流浪汉,所以我在全局命名空间中这样做.

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;

但是突然编译器不再喜欢我了.因为有一个 ::string(我的类)和 another ::string(标准的,包含在你的头文件中并被引入全局命名空间与 using namespace std;),会有各种各样的痛苦.

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.

更糟糕的是,这个问题会通过包含 my 标头(包括您的标头,...您明白了.)的每个文件得到提升.

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.

命名空间是为了避免标识符冲突.您的标头不仅将 MyStuff 引入全局命名空间,还引入了 stringfstream 中的每个标识符.有可能我们中的大多数人实际上从未真正需要它们,那么为什么将它们拖到全球范围内,污染环境?

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?

补充:从维护编码器/调试器的角度来看,foo::MyStuffMyStuff,namespace'方便十倍d 其他地方(甚至可能不是同一个源文件),因为您可以在代码中需要它的地方获得命名空间信息.

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.

这篇关于使用命名空间标准;在头文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Bring window to front -gt; raise(),show(),activateWindow() don’t work(把窗户放在前面 -raise(),show(),activateWindow() 不起作用)
How to get a list video capture devices NAMES (web cameras) using Qt (crossplatform)? (C++)(如何使用 Qt(跨平台)获取列表视频捕获设备名称(网络摄像机)?(C++))
How to compile Qt as static(如何将 Qt 编译为静态)
C++ over Qt : Controlling transparency of Labels and Buttons(C++ over Qt:控制标签和按钮的透明度)
How to know when a new USB storage device is connected in Qt?(Qt如何知道新的USB存储设备何时连接?)
What is an event loop in Qt?(Qt 中的事件循环是什么?)