dimanche 29 mars 2015

C++ iostream operator overladed function return type


I'm still in learning phase of basic formats and commands of C++. I'm now at class operator function overloading and came to << and >>. My question is: when they are defined in friend functions such as below:



ostream &operator << ( ostream &output, const PhoneNumber &number )


and are called with PhoneNumber class phone like this:



cout << phone << endl;


Why is the friend function returning ostream&? I mean when a function returns a value of a particular type, it is generally received by a fundamental type variable such as bool, int, char, string, and etc. However, for ostream and istream, the returned type of ostream& is not being saved. Then, in this case, shouldn't it be void (carry out the task and terminate without returning any values)?




Aucun commentaire:

Enregistrer un commentaire