I know this is pretty basic but I’ve kind of been guessing up to this point.
Foo is just an object with private inheritance from the string class which is why I cast it. Using examples from Primer C++ (Prata’s)
So if I have a function like:
istream & operator>>(istream & is, Foo & f)
{
is >> (string &)f;
return is;
}
int main()
{
Foo f;
cin >> f;
}
So, once cin >> f is hit, the function is called and the now string is stored in the istream reference. The istream object is now returned and now…? Is the returned istream object’s content (the string) now placed automatically within f? Or did I miss a step in understanding how cin works?
Also, if I were to do:
int x;
cin >> f >> x;
How would it implicitly look like? Like (cin) >> x?
Finally, one more simple thing. If in a function (which contains an ostream reference) I am in a loop passing over every array item and doing this:
for(int i=0;i < 5;i++)
{
os << array[i] << "\n";
}
Is the ostream object just compounded each array item within itself?
It becomes very easy if you replace the overloaded operators with normal function calls.
cin >> fsimply translates to:Now think of
cin >> f >> xas(cin >> f) >> x. This translates to:Since the first operator returns
cin, during execution, this actually becomes:Primitive data types such as
intare generally overloaded within the stream class itself, so it actually looks like this:You can extend this by yourself to answer your question about
ostream– it works exactly the same way.