title: function returning this pointer in c++
body: <p>1st code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  class demo
  {
    int a;
  public:
    demo():a(9){}
    demo&amp; fun()//return type isdemo&amp;
    {
      return *this;
    }
  };
  
  int main()
  {
    demo obj;
    obj.fun();
    return 0;
  }
  </code></pre>
  
  <p>2nd code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  class demo
  {
    int a;
  public:
    demo():a(9){}
    demo fun()//return type is demo
    {
      return *this;
    }
  };
  
  int main()
  {
    demo obj;
    obj.fun();
    return 0;
  }
  </code></pre>
  
  <p>what is the difference between these two codes as both are working in gcc?i am new here so forgive me if my way of asking is wrong.</p>