本文介绍了头文件和'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

推荐答案



不要。


你强制使用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.





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

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

也出现在那里,使得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'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 12:57