枚举类可以嵌套吗? [英] Can enum class be nested?
本文介绍了枚举类可以嵌套吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可以做到吗?
enum A
{
enum B
{
SOMETHING1,
SOMETHING2
};
enum C
{
SOMETHING3,
SOMETHING4
};
};
如果没有其他解决方案?
If not is there an alternative solution?
此问题的目的:希望/需要执行以下操作:
The purpose of this question: Want/need to be able to do something like this:
enum class ElementaryParticleTypes
{
enum class MATTER
{
enum class MESONS
{
PI
};
enum class BARYONS
{
PROTON,
NEUTRON
};
enum class LEPTONS
{
ELECTRON
};
};
enum class ANTI_MATTER
{
enum class ANTI_MESONS
{
ANTI_PI
};
enum class ANTI_BARYONS
{
ANTI_PROTON
ANTI_NEUTRON
};
enum class ANTI_LEPTONS
{
POSITRON
};
};
};
希望使用强类型功能。
推荐答案
否,它们不能以这种方式嵌套。实际上,任何编译器都会拒绝它。
No, they cannot be nested that way. In fact, any compiler would reject it.
如果没有其他解决方案?
If not is there an alternative solution?
这主要取决于您要实现的目标(解决什么问题?)。如果您的目标是能够编写 A :: B :: SOMETHING1
之类的东西,则可以通过以下方式在名称空间中定义它们:
That mostly depends on what you are trying to achieve (solution to what problem?). If your goal is to be able to write something like A::B::SOMETHING1
, you could just define them within a namespace, this way:
namespace A
{
enum B
{
SOMETHING1,
SOMETHING2
};
enum C
{
SOMETHING3,
SOMETHING4
};
}
这篇关于枚举类可以嵌套吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文