久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用命名空間標準;在頭文件中

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.

這篇關于使用命名空間標準;在頭文件中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 成人水多啪啪片 | 激情av免费看 | 欧美另类视频在线 | 九九亚洲精品 | 亚洲精品日韩一区二区电影 | 国产一区二区三区视频 | 国产精品久久久久久婷婷天堂 | 亚洲人成在线播放 | 我要看免费一级毛片 | 久久久爽爽爽美女图片 | 日韩精品视频在线观看一区二区三区 | 国产在线精品一区 | 久久国产精品视频免费看 | 日本一区二区高清不卡 | 亚洲欧美日韩电影 | 一级做a爰片久久毛片 | 蜜桃久久| 黄色a视频 | 亚洲一区二区三区在线视频 | 日韩欧美操 | 日韩精品在线视频 | 国产精品久久久久久久久久久久 | 欧美一区视频 | 日批av| 人人干人人艹 | 成人欧美一区二区三区黑人孕妇 | 天天干亚洲 | 亚洲国产精品久久久久秋霞不卡 | 黄色一级大片在线观看 | 成人在线免费电影 | 91动漫在线观看 | 拍真实国产伦偷精品 | 成人毛片在线视频 | 欧美亚洲一区二区三区 | 成人福利视频网站 | 精品一区二区三区在线观看 | 国内精品在线视频 | 成人a免费 | 日韩视频中文字幕 | 欧美日韩国产精品一区二区 | 亚洲永久免费观看 |