为什么我们必须使用字符串头定义命名空间? [英] Why do we have to define a namespace with string header?

查看:48
本文介绍了为什么我们必须使用字符串头定义命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个非常基本的问题,但我无法弄清楚或发现

我们为什么要指定一个命名空间,这样的答案


#include< string>

使用命名空间std;


使用标准字符串库?这真的有必要,为什么?

谢谢,


Bernhard Enders。

解决方案

bgeneto写道:

我知道这是一个非常基本的问题,但我无法弄清楚或找到答案为什么我们有在使用标准字符串库时,使用命名空间std;

指定一个名称空间,比如这个

#include< string>
这真的有必要,为什么?
谢谢,



因为标准库函数是在名称空间标准库中声明的。


你必须完全限定在命名空间中声明的内容,否则

编译器找不到符号。


我会避免使用毯子''使用命名空间''指令并使用完全

限定名称(行std :: cout)或使用特定使用指令

要从命名空间中使用的项目(使用std :: cout; for

例子。


-

Ian Collins。


bgeneto写道:

我知道这是一个非常基本的问题,但我无法弄清楚或找到答案我们为什么要这样做必须指定一个命名空间,比如这个

#include< string>
使用命名空间std;

使用标准字符串库时?这真的有必要,为什么?
谢谢,

Bernhard Enders。




创建库时,它通常是一个好的想要在自己的命名空间中定义任何符号

。这样做会严重降低您使用的名称与库中名称冲突的机会

您写的与其他库中的名称冲突。您可能会说,C ++标准库的设计者只是遵循良好的风格

并通过将所有符号放在命名空间中设置一个很好的示例

std。


-

Alan Johnson


感谢您的解释,如果我理解以及字符串类和

可能它的方法是在

标准标题< string>中的命名空间std中定义的。如果是这样,这样做有什么意义呢?与旧学校相比,它是否更好?b $ b? std听起来像是一个范围,

但是我们在没有范围标准的情况下定义了什么?没有课,没有

方法,什么都没有?

关于你使用完全合格名字的''建议',我认为我必须
必须除了std :: cout,std :: cin等之外还使用std :: string ... ...

有点累赘,没有?


谢谢再来一次,


Bernhard Enders。


I know that it''s a very basic question, but I can''t figure out or find
an answer to why do we have to specify a namespace, like this

#include<string>
using namespace std;

when using the standard string library? Is this really necessary, why?
Thanks,

Bernhard Enders.

解决方案

bgeneto wrote:

I know that it''s a very basic question, but I can''t figure out or find
an answer to why do we have to specify a namespace, like this

#include<string>
using namespace std;

when using the standard string library? Is this really necessary, why?
Thanks,


Because the standard library functions are declared in the namespace std.

You have to fully qualify something declared in a namespace, otherwise
the compiler won''t find the symbol.

I''d avoid the blanket ''using namespace'' directive and either use fully
qualified names (line std::cout) or use specific using directives for
the items you want to use from the namespace (using std::cout; for
example).

--
Ian Collins.


bgeneto wrote:

I know that it''s a very basic question, but I can''t figure out or find
an answer to why do we have to specify a namespace, like this

#include<string>
using namespace std;

when using the standard string library? Is this really necessary, why?
Thanks,

Bernhard Enders.



When creating a library, it is generally a good idea to put any symbols
you define in their own namespace. Doing so severely decreases the
chances that names that you use will conflict with names in the library
you write conflicting with names in others'' libraries. You might say
the designers of the C++ Standard Library were just following good style
and setting a good example by putting all their symbols in namespace
std.

--
Alan Johnson


Thanks for your explanation, if I understand well the string class and
possibly it''s methods are defined in the namespace std within the
standard header <string>. If so, what is the point in doing this? Is it
any better compared to the old school? std sounds like a scope to me,
but what do we have defined without the scope std? No class, no
methods, nothing?
About your ''suggestion'' to use full qualified names, I figure that I
must also use std::string in addition to std::cout, std::cin, etc... a
little cumbersome, no?

Thanks one more time,

Bernhard Enders.


这篇关于为什么我们必须使用字符串头定义命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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