Const Member function Vs Const Friend Function
<p>How this code modifies a value in a const function:</p>

<pre><code>#include&lt;iostream&gt;
using namespace std;
class Y;
class X{
  public:
    void access(Y &amp;y) const;
};

class Y{
    int d;
  public:
    friend void X::access(Y &amp;y) const;
    Y(){d=0;}
}; 

void X::access(Y &amp;y) const 
{
    cout&lt;&lt;"Y is "&lt;&lt;y.d&lt;&lt;endl;
    y.d=1;
    cout&lt;&lt;"Y is "&lt;&lt;y.d&lt;&lt;endl;
}

int main() {
    X x;
    Y y;
    x.access(y);
}
</code></pre>

<p>And this code gives an error:</p>

<pre><code>#include&lt;iostream&gt;

using namespace std;

class Y{
    int d;
  public:
    void set() const{d=0;}
};                          

int main() {
    Y y1;
    return 0;
}
</code></pre>
