Return type of << operator overloading function in C++
<p>I have create a program to overload &lt;&lt; operator for my class.
I have a doubt about the return type of the overloaded method.</p>

<p>When i am returning the  out from the overloaded operator function the program is working fine.</p>

<p>But when i am not returning any thing from the function the program crashing under one condition.</p>

<p>In many C++ resource i have read that the return type is necessary to print operators in cascading . </p>

<p>Condition 1 : Passing
   When i am using statement </p>

<p><code>// cout&lt;&lt;"\n"&lt;&lt;mv1&lt;&lt;mv2&lt;&lt;mv3;
</code>
Every thing is working fine. Passing without return from overloaded function.</p>

<p>Condition 2:Failing
When i am using statemtent
cout&lt;&lt;"\n"&lt;

<p>This i know that return was not there so the program crashed at runtime.</p>

<p>But the question is what made program run in Condition 1 . Even without the return statement was not present in the overloading function.</p>

<h1>Program below</h1>

<pre><code>I have create a program to overload &lt;&lt; operator for my class.
</code></pre>

<p>I have a doubt about the return type of the overloaded method.</p>

<p>When i am returning the  out from the overloaded operator function the program is working fine.</p>

<p>But when i am not returning any thing from the function the program crashing under one condition.</p>

<p>In many C++ resource i have read that the return type is necessary to print operators in cascading . </p>

<p>Condition 1 : Passing
   When i am using statement </p>

<p><code>// cout&lt;&lt;"\n"&lt;&lt;mv1&lt;&lt;mv2&lt;&lt;mv3;
</code>
Every thing is working fine. Passing without return from overloaded function.</p>

<p>Condition 2:Failing
When i am using statemtent
cout&lt;&lt;"\n"&lt;

<p>This i know that return was not there so the program crashed at runtime.</p>

<p>But the question is what made program run in Condition 1 . Even without the return statement was not present in the overloading function.</p>

<h1>Program below</h1>

<pre><code>#include &lt;iostream&gt;
#include &lt;stdio.h&gt;

using namespace std;

class myvar{

  private:
  int var_x,var_y;

  public:

  friend ostream&amp; operator&lt;&lt;(ostream &amp;out,myvar n);
  void setvalue(int x,int y)
  {
     var_x = x ;
     var_y = y ;
  }

};

ostream&amp; operator&lt;&lt;(ostream &amp;out,myvar n)
{
cout&lt;&lt;"("&lt;&lt;n.var_x&lt;&lt;","&lt;&lt;n.var_y&lt;&lt;")"; 
//return out;

}


int main()
{
 myvar mv1,mv2,mv3;
 mv1.setvalue(10,20);
 mv2.setvalue(30,40);
 mv3.setvalue(50,60);

//Working

    // cout&lt;&lt;"\n"&lt;&lt;mv1&lt;&lt;mv2&lt;&lt;mv3;

//Not Working

    // cout&lt;&lt;"\n"&lt;&lt;mv1&lt;&lt;mv2&lt;&lt;mv3&lt;&lt;"Hello"&lt;&lt;1243&lt;&lt;11.5;

}
</code></pre>