头文件和'using directive' [英] header file and 'using directive'

查看:112
本文介绍了头文件和'using directive'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下是在Test.h中


#ifndef TEST_H

#define TEST_H


#包括< iostream>

#include< string>


使用命名空间std;


class Test

{

public:

Test(const string& str):val(str)

{

cout<< val<<结束;

}


string val;

};

#endif


以上内容被编译器接受。但通常在头文件中没有提到''使用

声明/指令''。为什么呢?
如此?


谢谢

V.Subramanian

Suppose the following is in Test.h

#ifndef TEST_H
#define TEST_H

#include <iostream>
#include <string>

using namespace std;

class Test
{
public:
Test(const string& str) : val(str)
{
cout << val << endl;
}

string val;
};
#endif

The above is accepted by the compiler. But generally ''using
declaration/directives'' are not mentioned in header files. Why is it
so ?

Thanks
V.Subramanian

推荐答案

su ************* *@yahoo.com ,印度写道:
su**************@yahoo.com, India wrote:

假设以下是在Test.h中


# ifndef TEST_H

#define TEST_H


#include< iostream>

#include< string>


使用命名空间std;
Suppose the following is in Test.h

#ifndef TEST_H
#define TEST_H

#include <iostream>
#include <string>

using namespace std;



不要。


你强制使用using指令任何包含标题的文件。


为什么人们如此迷恋使用命名空间标准,而不是简单地使用完全限定名称?b $ b? br />

-

Ian Collins。

Don''t.

You force the using directive on any file that includes the header.

Why to people get so obsessed with "using namespace std", rather than
simply using fully qualified names?

--
Ian Collins.


< su ******** ****** @ yahoo.com在留言中写道

新闻:a1 *************************** ******* @ d21g2000 prf.googlegroups.com ...

:假设是followi ng在Test.h中



:#ifndef TEST_H

:#define TEST_H



:#include< iostream>

:#include< string>



:using namespace std;



:上课测试

:{

:公开:

:测试(const string& str):val(str)

:{

:cout<< val<<结束;

:}



:字符串val;

:};

:#endif



:编译器接受上述内容。但通常''使用

:声明/指令''在头文件中没有提到。

:为什么会这样?


因为它们违背命名空间的目的,在所有编译单元中创建

潜在的名称冲突

包括标题。


考虑以下两个示例文件:


//BobsLib.h

命名空间BobsLib {

class runtime_error {.. 。};

...

}

//MySourceFile.cpp

#include" BobsLib。 h"

#include" Test.h" //上面的文件


//我的文件密集使用BobsLib

使用命名空间BobsLib;


void myFunction ()

{

尝试{

//使用BobsLib的东西......

}

catch(runtime_error& x); // ## ouch

// runtime_error模棱两可:std ::〜或BobsLib ::〜?

}

基本上,但放一个在头文件的全局

范围内使用指令,可以防止该

头的所有用户在他们自己的代码中安全地使用该功能。

不太好。

我希望这会有所帮助,

Ivan


-
http://ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格

Brainbench MVP for C ++< http:// www。 brainbench.com

<su**************@yahoo.comwrote in message
news:a1**********************************@d21g2000 prf.googlegroups.com...
: Suppose the following is in Test.h
:
: #ifndef TEST_H
: #define TEST_H
:
: #include <iostream>
: #include <string>
:
: using namespace std;
:
: class Test
: {
: public:
: Test(const string& str) : val(str)
: {
: cout << val << endl;
: }
:
: string val;
: };
: #endif
:
: The above is accepted by the compiler. But generally ''using
: declaration/directives'' are not mentioned in header files.
: Why is it so ?

Because they go against the purpose of namespaces, creating
potential name collisions in all compilation units that
include the header.

Consider the following two example files:

//BobsLib.h
namespace BobsLib {
class runtime_error { ... };
...
}
//MySourceFile.cpp
#include "BobsLib.h"
#include "Test.h" // your file above

// My file intensively uses BobsLib
using namespace BobsLib;

void myFunction()
{
try {
//stuff using BobsLib...
}
catch( runtime_error& x ); //## ouch
// runtime_error ambiguous: std::~ or BobsLib::~ ?
}
Basically, but putting a using directive in the global
scope of a header file, you prevent all users of that
header from safely using that feature in their own code.
Not nice.
I hope this helps,
Ivan

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com


On 5 ??×,15:44," subramanian10 ... @ yahoo.com,India"

< subramanian10 ... @ yahoo.comwrote:
On 5 ??×, 15:44, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:

以上内容已被编译器接受。但通常在头文件中没有提到''使用

声明/指令''。为什么这么多?b $ b?
The above is accepted by the compiler. But generally ''using
declaration/directives'' are not mentioned in header files. Why is it
so ?



因为当您在源代码中包含头文件时(那就是

与复制粘贴整个头文件相同) ,使用指令

也出现在那里,使得std名称空间意外地显示为没有任何限定条件的状态。

Because when you include header file in your source code (that''s the
same as copy-pasting whole header file there), the using directive
appears there as well, making std namespace unexpectedily visible to
your statemsnts without any qualifying.


这篇关于头文件和'using directive'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆