转发在c ++中声明一个枚举 [英] Forward declaring an enum in c++

查看:130
本文介绍了转发在c ++中声明一个枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试做如下事情:

enum E;

void Foo(E e);

enum E {A, B, C};

。我在谷歌快速浏览,一致似乎是你不能做到,但我不明白为什么。任何人都可以解释?非常感谢。

which the compiler rejects. I've had a quick look on Google and the consensus seems to be "you can't do it", but I can't understand why. Can anyone explain? Many thanks.

澄清2:我这样做,因为我有一个类中的私人方法,采取所述枚举,我不希望枚举的值暴露 - 所以,例如,我不想让任何人知道E定义为

Clarification 2: I'm doing this as I have private methods in a class that take said enum, and I do not want the enum's values exposed - so, for example, I do not want anyone to know that E is defined as

enum E {
    FUNCTIONALITY_NORMAL, FUNCTIONALITY_RESTRICTED, FUNCTIONALITY_FOR_PROJECT_X
}

作为项目X不是我想让用户知道的东西。

as project X is not something I want my users to know about.

所以,我想转发声明枚举,所以我可以将私有方法放在头文件中,在cpp内部声明枚举,并分发构建的库文件

So, I wanted to forward declare the enum so I could put the private methods in the header file, declare the enum internally in the cpp, and distribute the built library file and header to people.

推荐答案

枚举不能向前声明的原因是,在不知道值的情况下,编译器不能知道枚举变量所需的存储。 C ++编译器允许基于包含所有指定值所需的大小指定实际存储空间。如果所有可见的是前向声明,翻译单元不知道选择了什么存储大小 - 它可以是一个char或一个int,或其他。

The reason the enum can't be forward declared is that without knowing the values, the compiler can't know the storage required for the enum variable. C++ Compiler's are allowed to specify the actual storage space based on the size necessary to contain all the values specified. If all that is visible is the forward declaration, the translation unit can't know what storage size will have been chosen - it could be a char or an int, or something else.

从ISO C ++标准的第7.2.5节:

From Section 7.2.5 of the ISO C++ Standard:


>底层类型是一个整数类型,可以表示枚举中定义的所有枚举值。它是实现定义的,其中整数类型用作枚举的基础类型,除非底层类型不应大于 int ,除非枚举器的值不能适合 int unsigned int 。如果 enumerator-list 为空,底层类型就好像枚举有一个枚举值为0的值。 sizeof()应用于枚举类型,枚举类型的对象或枚举器,是应用于底层类型的 sizeof()的值。

The underlying type of an enumeration is an integral type that can represent all the enumerator values defined in the enumeration. It is implementation-defined which integral type is used as the underlying type for an enumeration except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int. If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0. The value of sizeof() applied to an enumeration type, an object of enumeration type, or an enumerator, is the value of sizeof() applied to the underlying type.

由于函数的调用者必须知道正确设置调用堆栈的参数大小,枚举列表中的枚举数必须在函数原型之前已知。

Since the caller to the function must know the sizes of the parameters to correctly setup the call stack, the number of enumerations in an enumeration list must be known before the function prototype.

更新:
在C ++ 0X中,已经提出并接受了用于声明枚举类型的语法。您可以在 http://www.open-std。上查看提案。 org / jtc1 / sc22 / wg21 / docs / papers / 2008 / n2764.pdf

这篇关于转发在c ++中声明一个枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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