简体   繁体   中英

Return type of << operator overloading function in C++

I have create a program to overload << operator for my class. I have a doubt about the return type of the overloaded method.

When i am returning the out from the overloaded operator function the program is working fine.

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

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

Condition 1 : Passing When i am using statement

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

Condition 2:Failing When i am using statemtent cout<<"\\n"<

This i know that return was not there so the program crashed at runtime.

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

Program below

I have create a program to overload << operator for my class.

I have a doubt about the return type of the overloaded method.

When i am returning the out from the overloaded operator function the program is working fine.

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

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

Condition 1 : Passing When i am using statement

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

Condition 2:Failing When i am using statemtent cout<<"\\n"<

This i know that return was not there so the program crashed at runtime.

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

Program below

#include <iostream>
#include <stdio.h>

using namespace std;

class myvar{

  private:
  int var_x,var_y;

  public:

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

};

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

}


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

//Working

    // cout<<"\n"<<mv1<<mv2<<mv3;

//Not Working

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

}

This code has Undefined Behavior .
This is very bad. Usually it means that a crash will happen (but technically anything can happen).

ostream& operator<<(ostream &out,myvar n)
{
out<<"("<<n.var_x<<","<<n.var_y<<")";  // fixed cout to out.
//return out;

}

This is because you specify a return type in the function signature.

ostream& operator<<(ostream &out,myvar n)
^^^^^^^^

If your function does not contain a return keyword then your code is invalid. So for the function as defined (with the commented out return) your program will most likely crash.

If you change your code to:

void operator<<(ostream &out,myvar n)
{
out<<"("<<n.var_x<<","<<n.var_y<<")"; 
}

Now it will compile (be valid) and work.
But the consequence is you can not chain stream operations together.

 myvar  x;
 // Set some value in x

 // This will work fine.
 std::cout << x;

 // But this will fail to compile.
 std::cout << x << " Another Thing";

Because the first call to operator<< returns a void the second call to operator<< does not know what to do.

So your best bet is to return the stream to allow chaining.

ostream& operator<<(ostream &out,myvar n)
{
    out<<"("<<n.var_x<<","<<n.var_y<<")"; 
    return out;
}

PS: Some space characters would be really nice to aid readability.

// This is easier to read for a human.
std::ostream& operator<<(std::ostream &out, myvar n)
{
    out << "(" << n.var_x << "," << n.var_y << ")"; 
    return out;
}

Yes, you always return the passed in ostream and probably want to pass in a const reference to the value you want outputed:

ostream& operator<<(ostream &out, const myvar& n)
{
    out << "(" << n.var_x << "," << n.var_y << ")"; 
    return out;
} 

My biggest doubt is that why chaining of << in following statement

is working even without the return of out from overloaded function.

cout<<"\n"<<mv1<<mv2<<mv3;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM