title: Best way to implement globally scoped data
body: <p>I'd like to make program-wide data in a C++ program, without running into pesky <a href="http://msdn.microsoft.com/en-us/library/72zdcz6f%28VS.85%29.aspx" rel="nofollow noreferrer">LNK2005</a> errors when all the source files #includes this "global variable repository" file.</p>
  
  <p>I have 2 ways to do it in C++, and I'm asking <em>which way is better</em>.</p>
  
  <p>The easiest way to do it in C# is just public static members.</p>
  
  <p>C#:</p>
  
  <pre><code>public static class DataContainer
  {
      public static Object data1 ;
      public static Object data2 ;
  }
  </code></pre>
  
  <p>In C++ you can do the same thing</p>
  
  <h2>C++ global data way#1:</h2>
  
  <pre><code>class DataContainer
  {
  public:
      static Object data1 ;
      static Object data2 ;
  } ;
  Object DataContainer::data1 ;
  Object DataContainer::data2 ;
  </code></pre>
  
  <p>However there's also extern</p>
  
  <h2>C++ global data way #2:</h2>
  
  <pre><code>class DataContainer
  {
  public:
      Object data1 ;
      Object data2 ;
  } ;
  extern DataContainer * dataContainer ; // instantiate in .cpp file
  </code></pre>
  
  <p>In C++ <em>which is better</em>, or possibly another way which I haven't thought about?</p>
  
  <p>The solution has to not cause LNK2005 "object already defined" errors.</p>