title: Return the current object (*this) in C++?
body: <p>I have the following code:</p>
  
  <p>Code 1</p>
  
  <pre><code>class Student {
       int no;
       char grade[M+1];
   public:
       Student() {
           no = 0;
           grade[0] = '\0';
       }
       void set(int n, const char* g) {
           no = n;
           strcpy(grade, g);
  
       }
       const Student getObject() {
           return *this;
       }
       void display() const {
           cout &lt;&lt; no &lt;&lt; ", " &lt;&lt; grade &lt;&lt; endl;
       }
   };
  </code></pre>
  
  <p>Code 2:</p>
  
  <pre><code>// no change from code 1
  const Student&amp; getObject() {
           return *this;
       }
  // no change from code 1
  </code></pre>
  
  <p>As the book I am reading explains the difference in the getObject() of the code 1 and 2 is that the getObject() of code 2 returns a reference to the current object, instead of a copy (for efficiency reasons).</p>
  
  <p>However, I have tested (code 2) as follows:</p>
  
  <p>Tested code:</p>
  
  <pre><code>Student harry, harry1;
      harry.set(123, "ABCD");
  
      harry1 = harry.getObject();
      harry1.set(1111,"MMMMMM");
      harry.display(); // Line 1 =&gt; displayed: 123, ABCD
      harry1.display(); / Line 2 =&gt; displayed: 1111, MMMMMM
  </code></pre>
  
  <p>I dont get it. If the getObject() returns a reference, then Line 1 in the tested code should also display 111, MMMMMM? Because I thought that harry1 should contain the address of harry object???
  Or am I misunderstanding something?</p>