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

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

      <small id='2knBY'></small><noframes id='2knBY'>

        • <bdo id='2knBY'></bdo><ul id='2knBY'></ul>

      1. <i id='2knBY'><tr id='2knBY'><dt id='2knBY'><q id='2knBY'><span id='2knBY'><b id='2knBY'><form id='2knBY'><ins id='2knBY'></ins><ul id='2knBY'></ul><sub id='2knBY'></sub></form><legend id='2knBY'></legend><bdo id='2knBY'><pre id='2knBY'><center id='2knBY'></center></pre></bdo></b><th id='2knBY'></th></span></q></dt></tr></i><div id='2knBY'><tfoot id='2knBY'></tfoot><dl id='2knBY'><fieldset id='2knBY'></fieldset></dl></div>
      2. 在 C++ 类中初始化静态变量?

        Initialize static variables in C++ class?(在 C++ 类中初始化静态变量?)
            <bdo id='5Fz9d'></bdo><ul id='5Fz9d'></ul>

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

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

                  <tbody id='5Fz9d'></tbody>
                <tfoot id='5Fz9d'></tfoot>

                  <legend id='5Fz9d'><style id='5Fz9d'><dir id='5Fz9d'><q id='5Fz9d'></q></dir></style></legend>

                1. 本文介绍了在 C++ 类中初始化静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我注意到类中的某些函数实际上并未访问该对象,因此我将它们设为 static.然后编译器告诉我他们访问的所有变量也必须是静态的——嗯,到目前为止还可以理解.我有一堆字符串变量,例如

                  I have noticed that some of my functions in a class are actually not accessing the object, so I made them static. Then the compiler told me that all variables they access must also be static – well, quite understandable so far. I have a bunch of string variables such as

                  string RE_ANY = "([^\n]*)";
                  string RE_ANY_RELUCTANT = "([^\n]*?)";
                  

                  等等.然后我将它们全部设为 static const 因为它们永远不会改变.但是,我的程序只有在我将它们移出类时才会编译:否则,MSVC++2010 会抱怨只有静态常量整数变量可以在类中初始化".

                  and so on in the class. I have then made them all static const because they never change. However, my program only compiles if I move them out of the class: Otherwise, MSVC++2010 complains "Only static constant integral variables may be initialized within a class".

                  那很不幸.有解决方法吗?我想把他们留在他们所属的班级里.

                  Well that's unfortunate. Is there a workaround? I would like to leave them inside the class they belong to.

                  推荐答案

                  它们不能在类内部初始化,但可以在类外部初始化,在源文件中:

                  They can't be initialised inside the class, but they can be initialised outside the class, in a source file:

                  // inside the class
                  class Thing {
                      static string RE_ANY;
                      static string RE_ANY_RELUCTANT;
                  };
                  
                  // in the source file
                  string Thing::RE_ANY = "([^\n]*)";
                  string Thing::RE_ANY_RELUCTANT = "([^\n]*?)";
                  

                  更新

                  我刚刚注意到你问题的第一行 - 你不想想让这些函数static,你想让它们const.使它们 static 意味着它们不再与对象相关联(因此它们不能访问任何非静态成员),并使数据静态意味着它将与此类型的所有对象共享.这很可能不是您想要的.使它们 const 仅仅意味着它们不能修改任何成员,但仍然可以访问它们.

                  I've just noticed the first line of your question - you don't want to make those functions static, you want to make them const. Making them static means that they are no longer associated with an object (so they can't access any non-static members), and making the data static means it will be shared with all objects of this type. This may well not be what you want. Making them const simply means that they can't modify any members, but can still access them.

                  这篇关于在 C++ 类中初始化静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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++ 引用发生变化)

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

                      <tbody id='WNWx6'></tbody>
                    <legend id='WNWx6'><style id='WNWx6'><dir id='WNWx6'><q id='WNWx6'></q></dir></style></legend>

                      • <small id='WNWx6'></small><noframes id='WNWx6'>

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