title: Sharing static variables across files: namespace vs class
body: <p>There are a lot of questions about static vs global here but I think my question is a bit different.</p>
  
  <p>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.</p>
  
  <p>For example, I coded this:</p>
  
  <pre><code>//Foo.h
  class Foo
  {
    public:
    static int code;
    static int times_two(int in_);
  };
  
  namespace bar
  {
    static int kode;
  }
  </code></pre>
  
  <p>-</p>
  
  <pre><code>//Foo.cpp
  int Foo::code = 0;
  
  int Foo::times_two(int in_)
  {
    bar::kode++;
    code++;
    return 2*in_;
  }
  </code></pre>
  
  <p>-</p>
  
  <pre><code>//main.cpp
  int main()
  {
    cout &lt;&lt; "Foo::code = " &lt;&lt; Foo::code &lt;&lt; endl;
  
    for(int i=2; i &lt; 6; i++)
    {
      cout &lt;&lt; "2 x " &lt;&lt; i &lt;&lt; " = " &lt;&lt; Foo::times_two(i) &lt;&lt; endl;
      cout &lt;&lt; "Foo::code = " &lt;&lt; Foo::code &lt;&lt; endl;
      cout &lt;&lt; "bar::kode = " &lt;&lt; bar::kode &lt;&lt; endl;
  
      if(i == 3)
      {
        bar::kode++;
      }
    }
  }
  </code></pre>
  
  <p>All that yielded this for code and kode:</p>
  
  <pre><code>Foo::code = 1,2,3,4
  bar::kode = 0,0,1,1
  </code></pre>
  
  <p>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.</p>