<tfoot id='8px5l'></tfoot>

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

        <legend id='8px5l'><style id='8px5l'><dir id='8px5l'><q id='8px5l'></q></dir></style></legend>

        我可以在运行时在 C++ 中初始化静态常量成员吗?

        Can I initialize a static const member at run-time in C++?(我可以在运行时在 C++ 中初始化静态常量成员吗?)
        <tfoot id='6aLjG'></tfoot>

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

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

            • <legend id='6aLjG'><style id='6aLjG'><dir id='6aLjG'><q id='6aLjG'></q></dir></style></legend>
              • <bdo id='6aLjG'></bdo><ul id='6aLjG'></ul>
                  <tbody id='6aLjG'></tbody>

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

                  问题描述

                  是否可以在运行时初始化我的类的静态常量成员?这个变量在我的程序中是一个常量,但我想将它作为命令行参数发送.

                  Is it possible to initialize a static const member of my class during run-time? This variable is a constant throughout my program but I want to send it as a command-line argument.

                  //A.h
                  class A {
                  public: 
                      static const int T;
                  };
                  
                  //in main method
                  int main(int argc,char** argv)
                  {
                      //how can I do something like 
                      A::T = atoi(argv[1]);
                  }
                  

                  如果不能这样做,我应该使用什么类型的变量?我需要在运行时初始化它并保留常量属性.

                  If this cannot be done, what is the type of variable I should use? I need to initialize it at run-time as well as preserve the constant property.

                  推荐答案

                  我很抱歉不同意评论和答案的说法,即不可能在程序中初始化 static const 符号启动而不是编译时.

                  I am sorry to disagree with the comments and answers saying that it is not possible for a static const symbol to be initialized at program startup rather than at compile time.

                  实际上这是可能的,我多次使用它,但我从配置文件初始化它.类似的东西:

                  Actually this IS possible, and I used it many times, BUT I initialize it from a configuration file. Something like:

                  // GetConfig is a function that fetches values from a configuration file
                  const int Param1 = GetConfig("Param1");
                  const int MyClass::Member1 = GetConfig("MyClass.Member1");
                  

                  如您所见,这些静态常量在编译时不一定是已知的.它们可以从环境中设置,例如配置文件.

                  As you see, these static consts are not necessarily known at compile time. They can be set from the environment, such as a config file.

                  另一方面,从 argv[] 设置它们似乎非常困难,如果可行的话,因为当 main() 启动时,静态符号已经初始化.

                  On the other hand, setting them from argv[], seems very difficult, if ever feasible, because when main() starts, static symbols are already initialized.

                  这篇关于我可以在运行时在 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++ 引用发生变化)
                    <bdo id='ifG2G'></bdo><ul id='ifG2G'></ul>
                  • <i id='ifG2G'><tr id='ifG2G'><dt id='ifG2G'><q id='ifG2G'><span id='ifG2G'><b id='ifG2G'><form id='ifG2G'><ins id='ifG2G'></ins><ul id='ifG2G'></ul><sub id='ifG2G'></sub></form><legend id='ifG2G'></legend><bdo id='ifG2G'><pre id='ifG2G'><center id='ifG2G'></center></pre></bdo></b><th id='ifG2G'></th></span></q></dt></tr></i><div id='ifG2G'><tfoot id='ifG2G'></tfoot><dl id='ifG2G'><fieldset id='ifG2G'></fieldset></dl></div>
                    <tfoot id='ifG2G'></tfoot>
                        • <small id='ifG2G'></small><noframes id='ifG2G'>

                            <tbody id='ifG2G'></tbody>

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