Assignment to a C++ class method?
<p>I'm working on an small C++ example, to learn C++. Somebody wrote the main function already which looks like this:</p>

<pre><code>int main()
{
    map2d p1(1.,3.);       
    const map2d p2(0.,0.);

    p1.x();            // testing the access to member variable
    p2.x();            // testing the access to member variable

    p1.x() = 3.;       // changing the member variable

    return 0;
}
</code></pre>

<p>Okay. I created a class with the name map2d. Which works so far up to the point where the member variable has to be changed <code>p1.x() = 3.;</code>. My question is, how to do it? My class looks like this this:</p>

<pre><code>class map2d
{
    private: 
        double xp, yp;
    public:
        map2d (double xnew, double ynew): xp(xnew), yp(ynew) {}

        double x() const { return xp; }   // here is my problem
};
</code></pre>

<p>I was thinking to return a reference with <code>&amp;</code>, but this didn't worked out. I used something something like:</p>

<pre><code>double&amp; x() const { return xp; }
</code></pre>

<p>Did I make something wrong? Do you have any idea how to do it?</p>
