title: static const to an object in a shared library in C++. Is it share between processes?
body: <p>I've been writing a shared library in C++, but I want to share some instance of a class through users of the library. I mean, a read-only object loaded just one time from the library and used by every process linked to the library.</p>
  
  <p>As far as I know this can be made using <code>const</code> or <code>static const</code>, but it doesn't work as expected.</p>
  
  <p>For example:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  static const int x = 1;
  
  int main()
  {
      std::cout &lt;&lt; x &lt;&lt; std:endl;
  
      *(const_cast&lt;int *&gt;(&amp;x)) = 2;
      std::cout &lt;&lt; x &lt;&lt; std:endl;
  
      return 0;
  }
  </code></pre>
  
  <p>Using GCC 4.8.1 the code compiles well but, obviously, it fails at runtime because the <code>x</code> variable is read-only (it produces a segmentation fault on my Linux).</p>
  
  <p>However, lets see this code:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  struct A
  {
      A() : x(1) {}
      int x;
  }
  
  static const A a;
  
  int main()
  {
      std::cout &lt;&lt; a.x &lt;&lt; std:endl;
  
      const_cast&lt;A *&gt;(&amp;a)-&gt;x = 2;
      std::cout &lt;&lt; x &lt;&lt; std:endl;
  
      return 0;
  }
  </code></pre>
  
  <p>The last code compiles and run well. The executable prints</p>
  
  <pre><code>1
  2
  </code></pre>
  
  <p>I was able to modify the const data! So I guess the const modifier does NOT work properly with classes.</p>
  
  <p><strong>Then my questions are:</strong></p>
  
  <ol>
  <li>What do the <code>const static</code> modifiers do to an instance of a class?</li>
  <li>How could I put an instance in a shared library to share it through different processes, loading once and sharing the same RAM memory?</li>
  </ol>
  
  <p>The class I want to instantiate inherits from an abstract one.
  I am using C++11 but codes shown before were tested without the C++11 support.</p>
  
  <p><em>sorry if I made any english mistake</em></p>