0

for example,

istream &operator(istream &input,PhoneNumber &number)

(the PhoneNumber is a class made by me)

I'm very thankful if you can reply to me

香辛料
  • 1
  • 1
  • 3
    Because the stream state will change, and streams can't be copied. – πάντα ῥεῖ Mar 18 '22 at 12:16
  • 1
    what else should it be ? – 463035818_is_not_an_ai Mar 18 '22 at 12:18
  • 1
    typo? did you mean `operator>>` ? – 463035818_is_not_an_ai Mar 18 '22 at 12:19
  • 1
    Because that's how overloaded operators work. If you have a binary expression in the form `a op b` then the compiler will call the overloaded function as either `a.operator op(b)` or `operator op(a, b)`. Since you can't overload the "extraction" operator `>>` as a member of the input stream object, the second variant have to be used. And as mentioned, the the input stream state is both modified as well as it can't be copied, so it must be passed by reference. – Some programmer dude Mar 18 '22 at 12:20
  • This is explained in any beginner level C++ book listed [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). In particular, the first parameter is a reference because streams can't be copied. – Jason Mar 18 '22 at 12:21
  • The `number` parameter is a reference because the function is going to modify the object passed to it. – molbdnilo Mar 18 '22 at 12:22
  • @Someprogrammerdude thanks! I get it(*^▽^*) – 香辛料 Mar 18 '22 at 13:03
  • @πάνταῥεῖ thanks! I get it(*^▽^*) – 香辛料 Mar 18 '22 at 13:04

0 Answers0