pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

使用命名空間標(biāo)準(zhǔn);在頭文件中

using namespace std; in a header file(使用命名空間標(biāo)準(zhǔn);在頭文件中)
本文介紹了使用命名空間標(biāo)準(zhǔn);在頭文件中的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

所以,我在規(guī)范文件中有以下內(nèi)容

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

我在實(shí)現(xiàn)文件中也有

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

我正在收集應(yīng)用使用命名空間標(biāo)準(zhǔn);"在頭文件中是一個(gè)禁忌,完全合格是更好"的做法;例如 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;
}

而且,據(jù)我目前的理解,使用命名空間 std;在所有三個(gè)文件中,規(guī)范、實(shí)現(xiàn)和程序,本質(zhì)上將三個(gè)命名空間相互疊加,所以如果我在每個(gè)文件中分別聲明 string name;,編譯器將不知道哪個(gè)去什么.是真的嗎?

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?

我一般都明白,清楚是一件好事",但我不太清楚具體如何做,而我最感興趣的是更深層的為什么",這是這一切的基礎(chǔ).

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.

所以我的直接問(wèn)題是,在我提供的示例中,為編譯器和行業(yè)標(biāo)準(zhǔn)"描述函數(shù)的最清晰"方式是什么?而且,您能否將我引向更清楚地描述命名空間的推理和實(shí)際實(shí)現(xiàn)的資源.

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.

推薦答案

假設(shè)我自己聲明了一個(gè) string 類(lèi).因?yàn)槲沂且粋€(gè)懶惰的流浪漢,所以我在全局命名空間中這樣做.

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();
        // ...
};

一段時(shí)間后,我意識(shí)到重用一些您的代碼將使我的項(xiàng)目受益.感謝您將其開(kāi)源,我可以這樣做:

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;

但是突然編譯器不再喜歡我了.因?yàn)橛幸粋€(gè) ::string(我的類(lèi))和 another ::string(標(biāo)準(zhǔn)的,包含在你的頭文件中并被引入全局命名空間與 using namespace std;),會(huì)有各種各樣的痛苦.

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.

更糟糕的是,這個(gè)問(wèn)題會(huì)通過(guò)包含 my 標(biāo)頭(包括您的標(biāo)頭,...您明白了.)的每個(gè)文件得到提升.

Worse, this problem gets promoted through every file that includes my header (which includes your header, which... you get the idea.)

是的,我知道,在這個(gè)例子中,我也應(yīng)該因?yàn)闆](méi)有保護(hù)自己命名空間中的類(lèi)而受到責(zé)備,但那是我臨時(shí)想到的.

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.

命名空間是為了避免標(biāo)識(shí)符沖突.您的標(biāo)頭不僅將 MyStuff 引入全局命名空間,還引入了 stringfstream 中的每個(gè)標(biāo)識(shí)符.有可能我們中的大多數(shù)人實(shí)際上從未真正需要它們,那么為什么將它們拖到全球范圍內(nèi),污染環(huán)境?

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?

補(bǔ)充:從維護(hù)編碼器/調(diào)試器的角度來(lái)看,foo::MyStuffMyStuff,namespace'方便十倍d 其他地方(甚至可能不是同一個(gè)源文件),因?yàn)槟梢栽诖a中需要它的地方獲得命名空間信息.

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.

這篇關(guān)于使用命名空間標(biāo)準(zhǔn);在頭文件中的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫(xiě) for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線(xiàn)程)
Precise thread sleep needed. Max 1ms error(需要精確的線(xiàn)程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 比亚迪叉车-比亚迪电动叉车堆垛车托盘车仓储叉车价格多少钱报价 磁力去毛刺机_去毛刺磁力抛光机_磁力光饰机_磁力滚抛机_精密金属零件去毛刺机厂家-冠古科技 | 板框压滤机-隔膜压滤机-厢式压滤机生产厂家-禹州市君工机械设备有限公司 | 耐磨陶瓷,耐磨陶瓷管道_厂家-淄博拓创陶瓷科技 | FFU_空气初效|中效|高效过滤器_空调过滤网-广州梓净净化设备有限公司 | 塑胶跑道_学校塑胶跑道_塑胶球场_运动场材料厂家_中国塑胶跑道十大生产厂家_混合型塑胶跑道_透气型塑胶跑道-广东绿晨体育设施有限公司 | 达利园物流科技集团- | 安规_综合测试仪,电器安全性能综合测试仪,低压母线槽安规综合测试仪-青岛合众电子有限公司 | 一航网络-软件测评官网| 滁州高低温冲击试验箱厂家_安徽高低温试验箱价格|安徽希尔伯特 | 车件|铜件|车削件|车床加工|五金冲压件-PIN针,精密车件定制专业厂商【东莞品晔】 | 红酒招商加盟-葡萄酒加盟-进口红酒代理-青岛枞木酒业有限公司 | 蓄电池在线监测系统|SF6在线监控泄露报警系统-武汉中电通电力设备有限公司 | 杭州顺源过滤机械有限公司官网-压滤机_板框压滤机_厢式隔膜压滤机厂家 | 合肥风管加工厂-安徽螺旋/不锈钢风管-通风管道加工厂家-安徽风之范 | 集装箱箱号识别_自重载重图像识别_铁路车号自动识别_OCR图像识别 | 双段式高压鼓风机-雕刻机用真空泵-绍兴天晨机械有限公司 | 运动木地板厂家,篮球场木地板品牌,体育场馆木地板安装 - 欧氏运动地板 | 拉力机-拉力试验机-万能试验机-电子拉力机-拉伸试验机-剥离强度试验机-苏州皖仪实验仪器有限公司 | 成都亚克力制品,PVC板,双色板雕刻加工,亚克力门牌,亚克力标牌,水晶字雕刻制作-零贰捌广告 | 苗木价格-苗木批发-沭阳苗木基地-沭阳花木-长之鸿园林苗木场 | 管家婆-管家婆软件-管家婆辉煌-管家婆进销存-管家婆工贸ERP | 荣事达手推洗地机_洗地机厂家_驾驶式扫地机_工业清洁设备 | 苏州工作服定做-工作服定制-工作服厂家网站-尺品服饰科技(苏州)有限公司 | 碳纤维布-植筋胶-灌缝胶-固特嘉加固材料公司 | 硅胶管挤出机厂家_硅胶挤出机生产线_硅胶条挤出机_臣泽智能装备 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 光谱仪_积分球_分布光度计_灯具检测生产厂家_杭州松朗光电【官网】 | 回转支承-转盘轴承-回转驱动生产厂家-洛阳隆达轴承有限公司 | 起好名字_取个好名字_好名网免费取好名在线打分 | 电伴热系统施工_仪表电伴热保温箱厂家_沃安电伴热管缆工业技术(济南)有限公司 | 神超官网_焊接圆锯片_高速钢锯片_硬质合金锯片_浙江神超锯业制造有限公司 | 金属切削液-脱水防锈油-电火花机油-抗磨液压油-深圳市雨辰宏业科技发展有限公司 | 巨野月嫂-家政公司-巨野县红墙安康母婴护理中心 | 科客,主见不成见| 上海新光明泵业制造有限公司-电动隔膜泵,气动隔膜泵,卧式|立式离心泵厂家 | 运动木地板价格,篮球馆体育运动木地板生产厂家_欧氏地板 | 密集架-手摇-智能-移动-价格_内蒙古档案密集架生产厂家 | BAUER减速机|ROSSI-MERSEN熔断器-APTECH调压阀-上海爱泽工业设备有限公司 | 电缆接头_防水接头_电缆防水接头 - 乐清市新豪电气有限公司 | 沈飞防静电地板__机房地板-深圳市沈飞防静电设备有限公司 | 江苏全风,高压风机,全风环保风机,全风环形高压风机,防爆高压风机厂家-江苏全风环保科技有限公司(官网) | PSI渗透压仪,TPS酸度计,美国CHAI PCR仪,渗透压仪厂家_价格,微生物快速检测仪-华泰和合(北京)商贸有限公司 |