• <i id='6gJBx'><tr id='6gJBx'><dt id='6gJBx'><q id='6gJBx'><span id='6gJBx'><b id='6gJBx'><form id='6gJBx'><ins id='6gJBx'></ins><ul id='6gJBx'></ul><sub id='6gJBx'></sub></form><legend id='6gJBx'></legend><bdo id='6gJBx'><pre id='6gJBx'><center id='6gJBx'></center></pre></bdo></b><th id='6gJBx'></th></span></q></dt></tr></i><div id='6gJBx'><tfoot id='6gJBx'></tfoot><dl id='6gJBx'><fieldset id='6gJBx'></fieldset></dl></div>

  • <tfoot id='6gJBx'></tfoot>

    <legend id='6gJBx'><style id='6gJBx'><dir id='6gJBx'><q id='6gJBx'></q></dir></style></legend>

      <small id='6gJBx'></small><noframes id='6gJBx'>

      • <bdo id='6gJBx'></bdo><ul id='6gJBx'></ul>

      1. 为什么不允许静态常量浮点数?

        时间:2023-10-06

          <bdo id='I2VyQ'></bdo><ul id='I2VyQ'></ul>

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

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

              • <tfoot id='I2VyQ'></tfoot>
                • 本文介绍了为什么不允许静态常量浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个类,它本质上只是包含一堆在我的应用程序中使用的常量定义.但出于某种原因,long 可以编译但 float 不能:

                  I have a class which is essentially just holds a bunch of constant definitions used through my application. For some reason though, longs compile but floats do not:

                  class MY_CONSTS
                  {
                  public :
                      static const long   LONG_CONST = 1;      // Compiles 
                      static const float FLOAT_CONST = 0.001f; // C2864
                  };
                  

                  出现以下错误:

                  1>c:projectsmyprojectConstant_definitions.h(71) : error C2864: 'MY_CONSTS::FLOAT_CONST' : only static const integral data members can be initialized within a class
                  

                  我错过了什么吗?

                  推荐答案

                  回答你提出的实际问题:因为标准是这么说的".

                  To answer the actual question you asked: "because the standard says so".

                  只有静态、常量、整型 类型(包括枚举)的变量可以在类声明中初始化.如果编译器支持浮点数的内联初始化,则它是一个扩展.正如其他人指出的那样,处理静态、常量、非整型变量的方法是在类的相应源文件(而不是头文件)中定义和初始化它们.

                  Only variables of static, constant, integral types (including enumerations) may be initialized inside of a class declaration. If a compiler supports in-line initialization of floats, it is an extension. As others pointed out, the way to deal with static, constant, non-integral variables is to define and initialize them in the class's corresponding source file (not the header).

                  C++ 标准第 9.2 节类成员"第 4 项:

                  C++ Standard Section 9.2 "Class Members" item 4:

                  member-declarator 可以包含一个constant-initializer 仅当它声明了一个静态成员(9.4)const 积分或 const 枚举类型,见 9.4.2.

                  A member-declarator can contain a constant-initializer only if it declares a static member (9.4) of const integral or const enumeration type, see 9.4.2.

                  第 9.4.2 节静态数据成员"第 2 项:

                  Section 9.4.2 "Static Data Members" item 2:

                  如果静态数据成员是 const整数或常量枚举类型,它在类定义中的声明可以指定一个常量初始化器这应该是一个积分常数表达式 (5.19).在这种情况下,成员可以出现在积分常数中表达式.该成员仍将是如果它是在命名空间范围中定义的在程序和命名空间中使用范围定义不应包含初始化器.

                  If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression (5.19). In that case, the member can appear in integral constant expressions. The member shall still be defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer.

                  这篇关于为什么不允许静态常量浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么这个程序会崩溃:在 DLL 之间传递 std::stri 下一篇:如何从 dll 导出 C++ 类?

                  相关文章

                  最新文章

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

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

                    2. <small id='CxT49'></small><noframes id='CxT49'>