更改名称空间 [英] Changing namespaces

查看:82
本文介绍了更改名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原谅我这么快问了这么多问题。我只是在问我们书中没有回复的问题。


你可以在文件的一半更改名称空间吗?例如,如果

你想使用std然后中途切换到你自己:

{

...

使用命名空间std;

cout<<使用命名空间多边形做多边形;

;

cout<< poly1;

...

}


我知道这是一个荒谬的例子。为什么有人会为名为cout的3D对象定义

流出。但是,假设他们这样做了,这个代码片段会起作用吗?或者你是否只需要使用一个命名空间每个文件使用
并明确使用其他命名空间?

Forgive me for asking so many questions so quickly. I am simply
asking as they come up unanswered in my book.

Can you change namespaces half way through a file? For instance, if
you want to use std and then half way through switch to your own:
{
...
using namespace std;
cout << "doing polygons";
using namespace polygon;
cout << poly1;
...
}

I know that this is a ridiculous example. Why would anyone define an
outstream for a 3D object with the name cout. But, suppose they did,
would this code snippet work, or do you have to use just one namespace
per file and be explicit about the use of other namespaces?

推荐答案

2004年7月7日13:34:39 -0700,Blue Ocean< bl ********* @ hotmail.com> ;写道:
On 7 Jul 2004 13:34:39 -0700, Blue Ocean <bl*********@hotmail.com> wrote:
原谅我这么快就问了这么多问题。我只是问他们在我的书中没有得到答案。

你可以在文件中途改变命名空间吗?例如,如果你想使用std然后中途切换到你自己的:
{
...
使用命名空间std;
cout< < 做多边形;
使用命名空间多边形;
cout<< poly1;
...


我知道这是一个荒谬的例子。为什么有人会为名为cout的3D对象定义
外流。但是,假设他们这样做了,这个代码片段是否会起作用,或者你必须每个文件只使用一个命名空间并明确使用其他命名空间吗?
Forgive me for asking so many questions so quickly. I am simply
asking as they come up unanswered in my book.

Can you change namespaces half way through a file? For instance, if
you want to use std and then half way through switch to your own:
{
...
using namespace std;
cout << "doing polygons";
using namespace polygon;
cout << poly1;
...
}

I know that this is a ridiculous example. Why would anyone define an
outstream for a 3D object with the name cout. But, suppose they did,
would this code snippet work, or do you have to use just one namespace
per file and be explicit about the use of other namespaces?




您没有更改名称空间,您正在添加将被搜索的名称空间集合

。你可以拥有尽可能多的''使用命名空间......'',因为你需要搜索所有这些。所有这些都将被搜索。


在你的例子中,如果cout在命名空间std和polygon中定义,然后

你会得到一个编译错误''模糊名称cout''或者其他一些。


john



You aren''t changing namespaces, you are adding to the set of namespaces
that will be searched. You can have as many ''using namespace ...'' as you
like and all of them will be searched.

In your example, if cout was defined in namespace std and polygon, then
you would get a compiler error ''ambiguous name cout'' or some such.

john


Blue Ocean写道:
Blue Ocean wrote:
原谅我这么快就问了这么多问题。我只是问他们在我的书中没有得到答案。

你可以在文件中途改变命名空间吗?例如,如果你想使用std然后中途切换到你自己的:
{
...
使用命名空间std;
cout< < 做多边形;
使用命名空间多边形;
cout<< poly1;
...
}
Forgive me for asking so many questions so quickly. I am simply
asking as they come up unanswered in my book.

Can you change namespaces half way through a file? For instance, if
you want to use std and then half way through switch to your own:
{
...
using namespace std;
cout << "doing polygons";
using namespace polygon;
cout << poly1;
...
}




我认为您误解了使用指令。 using指令使命名空间中的
名称可用,就像它们已在

全局命名空间中声明一样。你的例子中的第二个using指令并没有
使第一个的效果无效。假设有一个多边形:: cout,

代码将无法编译,因为会有名称冲突。


因为你是一个Java程序员,这个例子可能会有所帮助:


import java.awt。*; // java等价的using指令。

import java.util。*;


public class Foo

{

public static void main(String [] args)

{

列表列表;

}

}


这个Java代码与你的C ++

例子有很多相同的问题。尝试编译它,你会看到我的意思。


-

Russell Hanneken
eu ******* @ cbobk.pbz

使用ROT13解码我的电子邮件地址。



I think you misunderstand using directives. A using directive makes
names from a namespace available as if they had been declared in the
global namespace. The second using directive in your example doesn''t
nullify the effect of the first one. Assuming there is a polygon::cout,
the code would fail to compile, because there would be a name conflict.

Since you''re a Java programmer, this example might help:

import java.awt.*; // The java equivalent of a using directive.
import java.util.*;

public class Foo
{
public static void main (String[] args)
{
List list;
}
}

This Java code suffers from pretty much the same problem as your C++
example. Try to compile it and you''ll see what I mean.

--
Russell Hanneken
eu*******@cbobk.pbz
Use ROT13 to decode my email address.


John Harrison写道:
John Harrison wrote:
2004年7月7日13:34:39 -0700,Blue Ocean< bl ******* **@hotmail.com>写道:
On 7 Jul 2004 13:34:39 -0700, Blue Ocean <bl*********@hotmail.com> wrote:
原谅我这么快就问了这么多问题。我只是问他们在我的书中没有得到答案。

你可以在文件中途改变命名空间吗?例如,如果你想使用std然后中途切换到你自己的:
{
...
使用命名空间std;
cout< < 做多边形;
使用命名空间多边形;
cout<< poly1;
...


我知道这是一个荒谬的例子。为什么有人会为名为cout的3D对象定义
外流。但是,假设他们这样做了,这个代码片段是否会起作用,或者你必须每个文件只使用一个命名空间并明确使用其他命名空间吗?
Forgive me for asking so many questions so quickly. I am simply
asking as they come up unanswered in my book.

Can you change namespaces half way through a file? For instance, if
you want to use std and then half way through switch to your own:
{
...
using namespace std;
cout << "doing polygons";
using namespace polygon;
cout << poly1;
...
}

I know that this is a ridiculous example. Why would anyone define an
outstream for a 3D object with the name cout. But, suppose they did,
would this code snippet work, or do you have to use just one namespace
per file and be explicit about the use of other namespaces?


<你没有改变命名空间,你正在添加要搜索的名称空间集。你可以拥有尽可能多的''使用命名空间......',并且所有这些都将被搜索。

在你的例子中,如果在命名空间std和多边形中定义了cout ,然后
你会得到一个编译错误''模棱两可的名字cout''或者其他一些。

john



You aren''t changing namespaces, you are adding to the set of namespaces
that will be searched. You can have as many ''using namespace ...'' as you
like and all of them will be searched.

In your example, if cout was defined in namespace std and polygon, then
you would get a compiler error ''ambiguous name cout'' or some such.

john




哇。所以如果你使用命名空间std使用
;
使用命名空间系统的
;

使用命名空间我的;


在代码的顶部,然后将搜索所有三个名称空间......和

假设没有冲突它们将被解决?

所以如果你有std:ios:in在你的代码中,你可以使用命名空间std更改它为



使用命名空间ios:

然后只需指定in in?



Wow.. So if you put:

using namespace std;
using namespace system;
using namespace mine;

at the top of your code then all three namespaces will be searched... and
assuming there is no conflict they will be resolved?

So if you have "std:ios:in" in your code, you could change it for
using namespace std:
using namespace ios:
and then just specify "in"?


这篇关于更改名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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