"cout<<" and operator overloading...

gud mor friends,

Please help me understand the below concept::

i have this below fun for << operator overloadin:

class Time
{
private:
int min;
int hr;

public:
Time(){std::cout<<"in default"<<std::endl;};
Time(int h,int m);
void reset(int h=0,int m=0);

friend std::ostream & operator<<(std::ostream & os,const Time & T);

void show() const;
};


ostream & operator<<(ostream & os,const Time & T)
{

os<<"HH:MM: "<<T.hr<<":"<<T.min<<"\n";
// cout<<os;
return os;
}


main()
{
Time tim1(9,30);
Time tim2(5,30);
cout<<"::Start::"<<endl;
cout<<tim1; //first call this ll be called as :ostream & operator<<(cout,tim1)

cout<<tim2<<" :time to go home\n";

//second call this ll be called as (ostream & operator<<(cout,tim1))<<" :time to go home\n";

...
}

when the first call is made where is the return value of ostream & recived for example:int sum(x,y) this fun ll be called as int res=sum(x,y) here the return value is recived in res and the fun call like sum(x,y) will show an error but in our prog "cout<<tim1" is running without any error.

The return value is what the following << operators are passed to. Once all of those are finished, that final return value is just ignored. Just like in your example, you might call sum(x,y) without assigning it to anything; the return value there is ignored.
Thanks zhuge knowing it ll ignore return value was a great relief...!!!
Topic archived. No new replies allowed.