如何从不同的命名空间获取枚举。 [英] How to get enum from a different namespace.

查看:105
本文介绍了如何从不同的命名空间获取枚举。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在某些命名空间中定义了一些枚举(枚举),而不是在
类中。如何使用整个命名空间在没有

的情况下在其他名称空间中使用枚举常量。


为了详细说明,枚举被声明为,

命名空间测试{

enum MyEnum {

VALUE1,VALUE2

};

}

现在在另一个命名空间中,

使用test :: MyEnum; //它得到了myenum。


MyEnum e = VALUE1; //它没有得到价值。

一个解决方案是使用test :: VALUE1;

但是因为这样的枚举const'的数量很大它不是可以单独使用



其他方法是使用命名空间测试;但是命名空间有很多其他的东西,我希望尽可能避免这样的陈述。


它看起来枚举常量是''免费的''命名空间,而不是由MyEnum守护的



无论如何要处理它?是否可以将它们包含在假的

类中并使用它们?

Hi,
I have some enum (enumeration ) defined in some namespace, not inside
class. How to use the enum constant''s in some other namespace without
using the whole namespace.

To say in little detail, the enum is declared as,
namespace test{
enum MyEnum{
VALUE1,VALUE2
};
}
now in another namespace,
using test::MyEnum; //It gets myenum.

MyEnum e = VALUE1; //It doesnt get the value.
One solution is to have using test::VALUE1;
but as such enum const''s are huge in number it is not possible to use
all of them seperately.
Other way is to have using namespace test; But the namespace has many
other things and I want to avoid such statement if possible.

It looks the enum constants are ''free'' inside the namespace, not
guarded by the MyEnum.

Anyway to deal with it? Is it possible to enclose them inside a dummy
class and use them ?

推荐答案

toton写道:
toton wrote:



我在某个命名空间中定义了一些enum(枚举),而不是在
类中。如何使用整个命名空间在没有

的情况下在其他名称空间中使用枚举常量。


为了详细说明,枚举被声明为,

命名空间测试{

enum MyEnum {

VALUE1,VALUE2

};

}

现在在另一个命名空间中,

使用test :: MyEnum; //它得到了myenum。


MyEnum e = VALUE1; //它没有得到价值。

一个解决方案是使用test :: VALUE1;

但是因为这样的枚举const'的数量很大它不是可以单独使用



其他方法是使用命名空间测试;但是命名空间有很多其他的东西,我希望尽可能避免这样的陈述。


它看起来枚举常量是''免费的''命名空间,而不是由MyEnum守护的



无论如何要处理它?
Hi,
I have some enum (enumeration ) defined in some namespace, not inside
class. How to use the enum constant''s in some other namespace without
using the whole namespace.

To say in little detail, the enum is declared as,
namespace test{
enum MyEnum{
VALUE1,VALUE2
};
}
now in another namespace,
using test::MyEnum; //It gets myenum.

MyEnum e = VALUE1; //It doesnt get the value.
One solution is to have using test::VALUE1;
but as such enum const''s are huge in number it is not possible to use
all of them seperately.
Other way is to have using namespace test; But the namespace has many
other things and I want to avoid such statement if possible.

It looks the enum constants are ''free'' inside the namespace, not
guarded by the MyEnum.

Anyway to deal with it?



你可以明确限定每个常数:


使用test :: MyEnum;

MyEnum e = test :: VALUE1;

You can explicitly qualify each constant:

using test::MyEnum;
MyEnum e = test::VALUE1;


是否可以将它们包含在虚拟类中并使用它们?
Is it possible to enclose them inside a dummy class and use them ?



我不知道你的意思,但我仍然至少可以回答

可能不是。


干杯! --M

I don''t know quite what you mean, but I can still at least answer
"probably not."

Cheers! --M




mlimber写道:

mlimber wrote:

toton写道:
toton wrote:



我在某个命名空间中定义了一些enum(枚举),而不是在
里面类。如何使用整个命名空间在没有

的情况下在其他名称空间中使用枚举常量。


为了详细说明,枚举被声明为,

命名空间测试{

enum MyEnum {

VALUE1,VALUE2

};

}

现在在另一个命名空间中,

使用test :: MyEnum; //它得到了myenum。


MyEnum e = VALUE1; //它没有得到价值。

一个解决方案是使用test :: VALUE1;

但是因为这样的枚举const'的数量很大它不是可以单独使用



其他方法是使用命名空间测试;但是命名空间有很多其他的东西,我希望尽可能避免这样的陈述。


它看起来枚举常量是''免费的''命名空间,而不是由MyEnum守护的



无论如何要处理它?
Hi,
I have some enum (enumeration ) defined in some namespace, not inside
class. How to use the enum constant''s in some other namespace without
using the whole namespace.

To say in little detail, the enum is declared as,
namespace test{
enum MyEnum{
VALUE1,VALUE2
};
}
now in another namespace,
using test::MyEnum; //It gets myenum.

MyEnum e = VALUE1; //It doesnt get the value.
One solution is to have using test::VALUE1;
but as such enum const''s are huge in number it is not possible to use
all of them seperately.
Other way is to have using namespace test; But the namespace has many
other things and I want to avoid such statement if possible.

It looks the enum constants are ''free'' inside the namespace, not
guarded by the MyEnum.

Anyway to deal with it?



您可以明确限定每个常量:


使用test :: MyEnum;

MyEnum e = test :: VALUE1;


You can explicitly qualify each constant:

using test::MyEnum;
MyEnum e = test::VALUE1;


是否可以将它们包含在虚拟类中并使用它们?
Is it possible to enclose them inside a dummy class and use them ?



我不知道你的意思,但我仍然可以至少回答

可能不是。


I don''t know quite what you mean, but I can still at least answer
"probably not."



是的,但实际上它在一个嵌套的命名空间内,在实际情况下

它看起来像MyEnum e = com :: ri: :inkserver :: server :: ui :: VALUE1 :(。

我不太喜欢。更改命名空间不在我的

手中,因为那些是不是我的代码!

另外我提到的看起来像

命名空间测试{

class MyClass {

public :

enum MyEnum {

VALUE1,VALUE2

};

};

}


使用test :: MyClass;

MyClass :: MyEnum e = MyClass :: VALUE1;

看起来更短。这里MyClass没有任何其他目的,而不是

持有''free''枚举常量(因为类隐藏了成员变量,

它也隐藏了枚举变量。但是C / C ++枚举不行!虽然Java / C#

enum这样做。)

因此,当这个类被使用时,它会附带所有成员

变量(静态& non static)并且无论如何都不需要通过完全限定来命名enum

valuse。


有什么建议吗?

Yes, but actually it is inside a nested namespace, and in actual case
it looks like MyEnum e = com::ri::inkserver::server::ui::VALUE1 :( .
Which I do not like very much. Changing the namespace is not in my
hand, as those are not my code!
The otherway I was mentioning looks like
namespace test {
class MyClass {
public:
enum MyEnum{
VALUE1,VALUE2
};
};
}

using test::MyClass;
MyClass::MyEnum e = MyClass::VALUE1;
Looks shorter. Here MyClass do not have any other purpose rather than
holding the ''free'' enum constants (As class hides the member variables,
it hides enum variables also. but C/C++ enum do not! Though Java/C#
enum do that).
Thus here when the class is ''used'' it comes with all of its member
variables (static & non static) and do not anyway need to name enum
valuse by fully qualifying it.

Any suggestion ?


干杯! --M
Cheers! --M


你好,

toton写道:
Hello,

toton wrote:

我在某个命名空间中定义了一些enum(枚举),而不是

里面的
。如何使用整个命名空间在没有

的情况下在其他名称空间中使用枚举常量。


为了详细说明,枚举被声明为,

命名空间测试{

enum MyEnum {

VALUE1,VALUE2

};

}

现在在另一个命名空间中,

使用test :: MyEnum; //它得到了myenum。


MyEnum e = VALUE1; //它没有得到价值。
I have some enum (enumeration ) defined in some namespace, not
inside
class. How to use the enum constant''s in some other namespace without
using the whole namespace.

To say in little detail, the enum is declared as,
namespace test{
enum MyEnum{
VALUE1,VALUE2
};
}
now in another namespace,
using test::MyEnum; //It gets myenum.

MyEnum e = VALUE1; //It doesnt get the value.



[...]

[...]


它看起来枚举常量是''免费''命名空间,而不是由MyEnum守护的

It looks the enum constants are ''free'' inside the namespace, not
guarded by the MyEnum.



枚举常量被放入枚举类型为

定义的范围内。这是从C继承的。它绝对是你的问题凿成石头的来源。

The enum constants are put into the scope where the enum type is
defined. That''s inherited from C. And it is definitely the source of
your problem chiseled into stone.


>

无论如何要处理它?是否可以将它们包含在假的

类中并使用它们?
>
Anyway to deal with it? Is it possible to enclose them inside a dummy
class and use them ?



如果你不想全部测试,那么也许你可以把

enums放到一个子命名空间作为一种解决方法。 />

名称空间测试{

名称空间枚举{

enum MyEnum {

VALUE1,VALUE2

};

}

}


使用命名空间test :: enums;


MyEnum e = VALUE1; //它确实得到了价值。


除此之外,你必须要对常数进行限定。


我不认为有任何意愿可以通过任何方式改变C ++,通过使用来吸引隐藏的名字。当使用一些

项而不是命名空间调用时,只需要该名称,而不是一堆

名称。如果将enum中的常量拉为

,那么这个清洁度将会丢失。无论如何,C ++从来没有设计用于保存每一点打字,所以你必须添加更多

名称空间或限定名称。

Bernd Strieder

If you do not want all from test, then perhaps you could put just the
enums into a subnamespace as a workaround.

namespace test{
namespace enums{
enum MyEnum{
VALUE1,VALUE2
};
}
}

using namespace test::enums;

MyEnum e = VALUE1; //It does get the value.

Other than that, you will have to qualify the constants.

I don''t think there is any will to change C++ in any way that somewhat
hidden names are pulled in via using. When using is called with some
item, not a namespace, then just that name is expected, not a bunch of
names. If pulling the enum in via using pulled its constants in as
well, then this cleanliness would be lost. Whatever, C++ has never
been designed to save every bit of typing, so you will have to add more
namespaces or qualify the names.

Bernd Strieder


这篇关于如何从不同的命名空间获取枚举。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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