• <bdo id='uSCqI'></bdo><ul id='uSCqI'></ul>

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

        <legend id='uSCqI'><style id='uSCqI'><dir id='uSCqI'><q id='uSCqI'></q></dir></style></legend>

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

      2. <tfoot id='uSCqI'></tfoot>

        在类定义中定义静态常量整数成员

        Defining static const integer members in class definition(在类定义中定义静态常量整数成员)
      3. <small id='o9VeO'></small><noframes id='o9VeO'>

        <tfoot id='o9VeO'></tfoot>

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

                <bdo id='o9VeO'></bdo><ul id='o9VeO'></ul>
                  <tbody id='o9VeO'></tbody>
                  本文介绍了在类定义中定义静态常量整数成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的理解是,C++ 允许在类中定义静态常量成员,只要它是整数类型即可.

                  My understanding is that C++ allows static const members to be defined inside a class so long as it's an integer type.

                  那么,为什么下面的代码会给我一个链接器错误?

                  Why, then, does the following code give me a linker error?

                  #include <algorithm>
                  #include <iostream>
                  
                  class test
                  {
                  public:
                      static const int N = 10;
                  };
                  
                  int main()
                  {
                      std::cout << test::N << "
                  ";
                      std::min(9, test::N);
                  }
                  

                  我得到的错误是:

                  test.cpp:(.text+0x130): undefined reference to `test::N'
                  collect2: ld returned 1 exit status
                  

                  有趣的是,如果我注释掉对 std::min 的调用,代码编译和链接就好了(即使 test::N 在前一行也被引用).

                  Interestingly, if I comment out the call to std::min, the code compiles and links just fine (even though test::N is also referenced on the previous line).

                  知道发生了什么吗?

                  我的编译器是 Linux 上的 gcc 4.4.

                  My compiler is gcc 4.4 on Linux.

                  推荐答案

                  我的理解是,C++ 允许在类中定义静态常量成员,只要它是整数类型即可.

                  My understanding is that C++ allows static const members to be defined inside a class so long as it's an integer type.

                  你说得对.您可以在类声明中初始化静态常量积分,但这不是定义.

                  You are sort of correct. You are allowed to initialize static const integrals in the class declaration but that is not a definition.

                  有趣的是,如果我注释掉对 std::min 的调用,代码编译和链接就好了(即使 test::N 在前一行也被引用).

                  Interestingly, if I comment out the call to std::min, the code compiles and links just fine (even though test::N is also referenced on the previous line).

                  知道发生了什么吗?

                  std::min 通过常量引用获取其参数.如果按值取它们,您就不会遇到这个问题,但由于您需要一个参考,因此您还需要一个定义.

                  std::min takes its parameters by const reference. If it took them by value you'd not have this problem but since you need a reference you also need a definition.

                  这是章节/诗句:

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

                  9.4.2/4 - 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.

                  有关可能的解决方法,请参阅 Chu 的回答.

                  See Chu's answer for a possible workaround.

                  这篇关于在类定义中定义静态常量整数成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                • <legend id='VAV7Q'><style id='VAV7Q'><dir id='VAV7Q'><q id='VAV7Q'></q></dir></style></legend>
                  <i id='VAV7Q'><tr id='VAV7Q'><dt id='VAV7Q'><q id='VAV7Q'><span id='VAV7Q'><b id='VAV7Q'><form id='VAV7Q'><ins id='VAV7Q'></ins><ul id='VAV7Q'></ul><sub id='VAV7Q'></sub></form><legend id='VAV7Q'></legend><bdo id='VAV7Q'><pre id='VAV7Q'><center id='VAV7Q'></center></pre></bdo></b><th id='VAV7Q'></th></span></q></dt></tr></i><div id='VAV7Q'><tfoot id='VAV7Q'></tfoot><dl id='VAV7Q'><fieldset id='VAV7Q'></fieldset></dl></div>
                    <tfoot id='VAV7Q'></tfoot>

                        • <bdo id='VAV7Q'></bdo><ul id='VAV7Q'></ul>
                            <tbody id='VAV7Q'></tbody>

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