Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because I may write <code>Foo</code> and its friend <code>Bar</code> (thus there is a trust relationship). </p> <p>But do I trust the people who write classes that are derived from <code>Bar</code>?<br> Not really. So they should not inherit friendship.</p> <p>Any change in the internal representation of a class will require a modification to anything that is dependent on that representation. Thus all members of a class and also all friends of the class will require modification. </p> <p>Therefore if the internal representation of <code>Foo</code> is modified then <code>Bar</code> must also be modified (because friendship tightly binds <code>Bar</code> to <code>Foo</code>). If friendship was inherited then all class derived from <code>Bar</code> would also be tightly bound to <code>Foo</code> and thus require modification if <code>Foo</code>'s internal representation is changed. But I have no knowledge of derived types (nor should I. They may even be developed by different companies etc). Thus I would be unable to change <code>Foo</code> as doing so would introduce breaking changes into the code base (as I could not modify all class derived from <code>Bar</code>).</p> <p>Thus if friendship was inherited you are inadvertently introducing a restriction on the ability to modify a class. This is undesirable as you basically render useless the concept of a public API.</p> <p>Note: A child of <code>Bar</code> can access <code>Foo</code> by using <code>Bar</code>, just make the method in <code>Bar</code> protected. Then the child of <code>Bar</code> can access a <code>Foo</code> by calling through its parent class.</p> <p>Is this what you want?</p> <pre><code>class A { int x; friend class B; }; class B { protected: // Now children of B can access foo void foo(A&amp; a, int n) { a.x = n; } }; class D : public B { public: foo(A&amp; a, int n) { B::foo(a, n + 5); } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload