• <tfoot id='0fdtG'></tfoot>

    <small id='0fdtG'></small><noframes id='0fdtG'>

    <legend id='0fdtG'><style id='0fdtG'><dir id='0fdtG'><q id='0fdtG'></q></dir></style></legend>

        <i id='0fdtG'><tr id='0fdtG'><dt id='0fdtG'><q id='0fdtG'><span id='0fdtG'><b id='0fdtG'><form id='0fdtG'><ins id='0fdtG'></ins><ul id='0fdtG'></ul><sub id='0fdtG'></sub></form><legend id='0fdtG'></legend><bdo id='0fdtG'><pre id='0fdtG'><center id='0fdtG'></center></pre></bdo></b><th id='0fdtG'></th></span></q></dt></tr></i><div id='0fdtG'><tfoot id='0fdtG'></tfoot><dl id='0fdtG'><fieldset id='0fdtG'></fieldset></dl></div>
          <bdo id='0fdtG'></bdo><ul id='0fdtG'></ul>
      1. 跨文件共享静态变量:命名空间与类

        Sharing static variables across files: namespace vs class(跨文件共享静态变量:命名空间与类)

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

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

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

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

                1. 本文介绍了跨文件共享静态变量:命名空间与类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这里有很多关于静态与全局的问题,但我认为我的问题有点不同.

                  There are a lot of questions about static vs global here but I think my question is a bit different.

                  我想知道是否有一种方法可以像类中的静态变量那样在文件之间共享放置在命名空间中的变量.

                  I want to know if there is a way to share a variable placed in a namespace across files the way static variables in a class can.

                  例如,我这样编码:

                  //Foo.h
                  class Foo
                  {
                    public:
                    static int code;
                    static int times_two(int in_);
                  };
                  
                  namespace bar
                  {
                    static int kode;
                  }
                  

                  -

                  //Foo.cpp
                  int Foo::code = 0;
                  
                  int Foo::times_two(int in_)
                  {
                    bar::kode++;
                    code++;
                    return 2*in_;
                  }
                  

                  -

                  //main.cpp
                  int main()
                  {
                    cout << "Foo::code = " << Foo::code << endl;
                  
                    for(int i=2; i < 6; i++)
                    {
                      cout << "2 x " << i << " = " << Foo::times_two(i) << endl;
                      cout << "Foo::code = " << Foo::code << endl;
                      cout << "bar::kode = " << bar::kode << endl;
                  
                      if(i == 3)
                      {
                        bar::kode++;
                      }
                    }
                  }
                  

                  代码和代码的所有结果:

                  All that yielded this for code and kode:

                  Foo::code = 1,2,3,4
                  bar::kode = 0,0,1,1
                  

                  再一次,有没有办法像类中的静态变量那样在文件之间共享放置在命名空间中的变量?我问的原因是因为我认为我可以通过使用 :: 符号来保护自己免受冲突的全局变量的影响,但我发现我不能.就像任何不尊重自己的程序员一样,我相信我做错了.

                  Once again, is there a way to share a variable placed in a namespace across files the way static variables in a class can? The reason I ask is because I thought I would be able to shield myself from confliciting global variables by using :: notation, and just found out I could not. And like any self-disrespecting programmer, I believe I am doing it wrong.

                  推荐答案

                  是:

                  //bar.h
                  namespace bar
                  {
                    extern int kode;
                  }
                  

                  classstruct 之外,static 具有完全不同的含义.它给出了一个符号内部链接.因此,如果您将相同的变量声明为 static,您实际上将获得所有翻译单元的不同副本,而不是唯一的全局变量.

                  Outside of a class or struct, static has a whole different meaning. It gives a symbol internal linkage. So if you declare the same variable as static, you will actually get a different copy for all translation units, not a unique global.

                  请注意,您需要初始化变量一次:

                  Note that you'll need to initialize the variable once:

                  //bar.cpp
                  namespace bar
                  {
                     int kode = 1337;
                  }
                  

                  这篇关于跨文件共享静态变量:命名空间与类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

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

                2. <legend id='OJcbS'><style id='OJcbS'><dir id='OJcbS'><q id='OJcbS'></q></dir></style></legend>
                  • <bdo id='OJcbS'></bdo><ul id='OJcbS'></ul>

                      <tfoot id='OJcbS'></tfoot>

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

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

                              <tbody id='OJcbS'></tbody>