title: Why do objects of the same class have access to each other's private data?
body: <p>Why do objects of the same class have access to each other's private data?</p>
  
  <pre><code>class TrivialClass {
  public: 
    TrivialClass(const std::string&amp; data) :
      mData(data) {};
  
    const std::string&amp; getData(const TrivialClass&amp; rhs) const {
      return rhs.mData;
    };
  
  private:
    std::string mData;
  };
  
  int main() {
    TrivialClass a("fish");
    TrivialClass b("heads");
  
    std::cout &lt;&lt; "b via a = " &lt;&lt; a.getData(b) &lt;&lt; std::endl;
    return 0;
  }
  </code></pre>
  
  <p>This codes works.  It is perfectly possible for object a to access private data from object b and return it.  Why should this be so?  I would think that private data is private.  (I started out by trying to understand copy constructors in the pimpl idiom, but then I discovered that I didn't even understand this simple situation.)</p>