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

        <tfoot id='cv136'></tfoot>

        在 C++ 中我应该把常量字符串放在哪里:静态类成员还是匿名命名空间?

        时间:2023-12-02

          <tbody id='NcrLy'></tbody>

          <tfoot id='NcrLy'></tfoot>

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

            1. <small id='NcrLy'></small><noframes id='NcrLy'>

              <legend id='NcrLy'><style id='NcrLy'><dir id='NcrLy'><q id='NcrLy'></q></dir></style></legend>
                • <bdo id='NcrLy'></bdo><ul id='NcrLy'></ul>
                • 本文介绍了在 C++ 中我应该把常量字符串放在哪里:静态类成员还是匿名命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要定义一些只能由一个类使用的常量字符串.看起来我有三个选择:

                  I need to define some constant strings that will be used only by one class. It looks like I have three options:

                  1. 将字符串直接嵌入到使用它们的位置.

                  1. Embed the strings directly into locations where they are used.

                  将它们定义为类的私有静态常量成员:

                  Define them as private static constant members of the class:

                  //A.h  
                  class A {  
                  private:  
                     static const std::string f1;  
                     static const std::string f2;  
                     static const std::string f3;  
                  };  
                  
                  //A.cpp  
                  const std::string f1 = "filename1";  
                  const std::string f2 = "filename2";  
                  const std::string f3 = "filename3";  
                  
                  //strings are used in this file  
                  

                • 在 cpp 文件的匿名命名空间中定义它们:

                • Define them in an anonymous namespace in the cpp file:

                  //A.cpp  
                  namespace {  
                    const std::string f1 = "filename1";  
                    const std::string f2 = "filename2";  
                    const std::string f3 = "filename3";  
                  }  
                  
                  //strings are used in this file  
                  

                • 鉴于这些选项,您会推荐哪一个?为什么?谢谢.

                  Given these options, which one would you recommend and why? Thanks.

                  推荐答案

                  我会将它们放在 CPP 文件的匿名命名空间中.它使它们对实现私有,同时使其对作为实现一部分的非成员函数可见(例如 operator<<).

                  I'd place them in anonymous namespace in the CPP file. It makes them private to the implementation and at the same moment makes it visible to the non-member functions that are part of implementation (such as operator<<).

                  这篇关于在 C++ 中我应该把常量字符串放在哪里:静态类成员还是匿名命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我可以在运行时在 C++ 中初始化静态常量成员吗? 下一篇:如何定义线程局部局部静态变量?

                  相关文章

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

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

                    1. <tfoot id='jxY6c'></tfoot>

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