开发者

Overloaded input operator

I have a function prototype for an input operator but I'm not sure what they all are and what they mean, I think istream is an object of type stream &sourceFileStream is being passed 开发者_开发技巧by reference. Can someone explain what each of the parameters mean ?

istream& operator >>(istream &sourceFileStream, Chart &aChart)


istream is an input stream: http://www.cplusplus.com/reference/iostream/istream/

Both sourceFileStream and aChart are being passed in by reference.


istream& is the return type, it returns the sourceFileStream parameter by reference after the function completes.

This is done so you can chain the operators (use them multiple times in the same statement).

You'd be more familiar with chaining output operators I'd bet... so, for example, you can use the << operator many times in this line:

int x;
std::cerr << std::hex << x << std::endl;

because it returns a reference so the stream (cerr) that it is manipulating.

istream& sourceFileStream as a parameter is an input stream (reading a file or something like that). It's passed by reference, so you're modifying the stream that gets passed in and then returning it for the reason stated above (most likely modifying it by reading forward in the reading and moving its internal pointers).

You're passing a chart object by reference as well, most likely to populate its internal data members from the stream contents. So, at the end of the use of this operator, your Chart's members will be populated from the stream contents as you specify in this function's definition. PS: by reference means that the object passed into this function will be modified directly as aChart will be an alias for that object. If it wasn't by reference, a copy of that object would be modified and this function would be useless.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜