I’m confused on why exactly I need to use references for the return type and parameter list in this example from my book below. Is their any reason besides that it takes up less memory than having everything being copied over using pass by value? Or does it have to deal more with if I wanted to do cascading?
istream &operator>>( stream &input, PhoneNumber &number)
{
//input whatever
return input;
}
Because a) streams are not copyable, b) getting input from a stream means mutating it, so you need to modify the original and not a copy (however would that be realised). And reference to
PhoneNumbershould be obvious — you’re getting input from the stream and into that object. If you’d pass it by copy, it wouldn’t be visible outside of the operator, which makes the entire exercise rather pointless.