title: Why is it possible to add functions to external library to access internal data
body: <p>this week i needed a feature in our internal static library but i was too lazy to recompile the library so i just modified the header file and was able to get access to private members. The same was possible to do when the library is a Windows dll.</p>
  <p>An example header might look like:</p>
  <pre><code>#pragma once
  
  #ifdef DLL
  #define DLL_INTERFACE __declspec(dllexport)
  #else
  #define DLL_INTERFACE
  #endif
  
  class DLL_INTERFACE foo
  {
  public:
      foo(); // init foo::a and foo::b to 0
  
      int getA() const;
      void setA(int a); // set foo::a to a and foo::b to a+2
  private:
      int a;
      int b;
  };
  </code></pre>
  <p>Now i added my own functions to the the header that allow me access to its private members like so:</p>
  <pre><code>class DLL_INTERFACE foo
  {
  public:
      foo(); // init foo::a and foo::b to 0
  
      int getA() const;
      void setA(int a); // set foo::a to a and foo::b to a+2
  
      int getB() const
      {
          return b;
      }
      void setB(int b)
      {
          this-&gt;b = b;
      }
  private:
      int a;
      int b;
  };
  </code></pre>
  <p>While testing my new functionality and recompiling the library (to not rely on that hack) i realized that this could compromise the internal state of the object.</p>
  <p>Why is this allowed/possible to change the header files of a compiled library (dll or static lib)? (I had expected that the compiler or the linker would complain that the object from the library does not match the object defined in the header)</p>
  <p>Thanks in advance</p>