<tfoot id='j0ADH'></tfoot>
      <bdo id='j0ADH'></bdo><ul id='j0ADH'></ul>
  1. <i id='j0ADH'><tr id='j0ADH'><dt id='j0ADH'><q id='j0ADH'><span id='j0ADH'><b id='j0ADH'><form id='j0ADH'><ins id='j0ADH'></ins><ul id='j0ADH'></ul><sub id='j0ADH'></sub></form><legend id='j0ADH'></legend><bdo id='j0ADH'><pre id='j0ADH'><center id='j0ADH'></center></pre></bdo></b><th id='j0ADH'></th></span></q></dt></tr></i><div id='j0ADH'><tfoot id='j0ADH'></tfoot><dl id='j0ADH'><fieldset id='j0ADH'></fieldset></dl></div>
  2. <small id='j0ADH'></small><noframes id='j0ADH'>

    1. <legend id='j0ADH'><style id='j0ADH'><dir id='j0ADH'><q id='j0ADH'></q></dir></style></legend>

      在 C++ 中前向声明枚举

      时间:2023-08-26

      <small id='wwbMT'></small><noframes id='wwbMT'>

        <tbody id='wwbMT'></tbody>

        <legend id='wwbMT'><style id='wwbMT'><dir id='wwbMT'><q id='wwbMT'></q></dir></style></legend>
        • <bdo id='wwbMT'></bdo><ul id='wwbMT'></ul>
            <i id='wwbMT'><tr id='wwbMT'><dt id='wwbMT'><q id='wwbMT'><span id='wwbMT'><b id='wwbMT'><form id='wwbMT'><ins id='wwbMT'></ins><ul id='wwbMT'></ul><sub id='wwbMT'></sub></form><legend id='wwbMT'></legend><bdo id='wwbMT'><pre id='wwbMT'><center id='wwbMT'></center></pre></bdo></b><th id='wwbMT'></th></span></q></dt></tr></i><div id='wwbMT'><tfoot id='wwbMT'></tfoot><dl id='wwbMT'><fieldset id='wwbMT'></fieldset></dl></div>
              • <tfoot id='wwbMT'></tfoot>
                本文介绍了在 C++ 中前向声明枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试执行以下操作:

                I'm trying to do something like the following:

                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?

                澄清 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.

                至于编译器 - 它是 GCC.

                As for the compiler - it's GCC.

                推荐答案

                枚举不能前向声明的原因是,在不知道值的情况下,编译器无法知道枚举变量所需的存储空间.允许 C++ 编译器根据包含所有指定值所需的大小来指定实际存储空间.如果所有可见的是前向声明,则翻译单元无法知道选择了多大的存储大小——它可能是 charint 或其他东西否则.

                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++ compilers 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 has 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,除非枚举器的值不能放入 intunsigned 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 set up the call stack, the number of enumerations in an enumeration list must be known before the function prototype.

                更新:

                在 C++0X 中,已经提出并接受了一种用于前向声明枚举类型的语法.您可以在 Forward 查看该提案枚举声明(修订版 3)

                In C++0X, a syntax for forward declaring enum types has been proposed and accepted. You can see the proposal at Forward declaration of enumerations (rev.3)

                这篇关于在 C++ 中前向声明枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 C++ 中使用枚举作为标志? 下一篇:C++:将枚举值打印为文本

                相关文章

                <legend id='5N5We'><style id='5N5We'><dir id='5N5We'><q id='5N5We'></q></dir></style></legend>
                    <bdo id='5N5We'></bdo><ul id='5N5We'></ul>
                  <tfoot id='5N5We'></tfoot>

                  <small id='5N5We'></small><noframes id='5N5We'>

                1. <i id='5N5We'><tr id='5N5We'><dt id='5N5We'><q id='5N5We'><span id='5N5We'><b id='5N5We'><form id='5N5We'><ins id='5N5We'></ins><ul id='5N5We'></ul><sub id='5N5We'></sub></form><legend id='5N5We'></legend><bdo id='5N5We'><pre id='5N5We'><center id='5N5We'></center></pre></bdo></b><th id='5N5We'></th></span></q></dt></tr></i><div id='5N5We'><tfoot id='5N5We'></tfoot><dl id='5N5We'><fieldset id='5N5We'></fieldset></dl></div>