>> and << operator overload

I just made a quiz for my programming class and misunderstood this question:

The return function type operator overload <<should be a reference to an ostream object.

That doesn't seem right to me at all. Of course, C ++ is a little more open than this. But I thought I would ask anyway. How is this right (or wrong)? C ++ knowledge really begins to fade when it comes to operator overloading.

+3
source share
6 answers

C ++ does not require the return type to be an object reference ostream. However, if you are trying to do something like:

cout << instance_of_custom_type << 3 << "hi" << endl;

:

ostream &operator << (ostream &os, custom_type &t);

, - , - :

BigInt operator << (const BigInt &i, unsigned int shift);

, << . 1 << 8 256, . ++ ( ) ostream, "" . , , - , , , : , ++ << - .

+16

, , ,

mystream &operator << (mystream &os, myclass &myobject){
   // do whatever
   return os;
}

mystream << myobject << fundamental_type_object;
+2

<< ostream.

"" , , "" - ? , , object chaining iostreams.

+2

operator<< , , operator=.

<iostreams> std::ostream.

+1

, . cout << 1; cout << " is a number"; cout << endl

0

. iostreams, , , . iostreams , , . , -, , , , , , - .

0

Source: https://habr.com/ru/post/1772424/


All Articles