TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: KURT KUZBA
from: BALOG PAL
date: 1997-09-28 22:28:00
subject: cout`ing an object

                               Hi, Kurt Kuzba!
On 24 Sep 97  03:19:52 you wrote to Jamie Kowinsky
 KK> friend ostream &operator<<(ostream&, const MathCalc&);
 KK> // This function is an ostream friend and has access to
 KK> // ostream functions. It returns an ostream object which
 KK> // is created within the friend function
 KK> ostream &operator<<(ostream &O, const MathCalc &MC)
 KK> {
 KK> // This function creates an ostream object. It accepts an
 KK> // ostream object and a data object. It then formats the
 KK> // data in the data object to an ostream format and returns
 KK> // the ostream to the calling function.
 KK> O << MC.iVar1 << " " << MC.cOperation << " " << MC.iVar2
 KK> << " = " << MC.iResult << "\n\r" << flush;
 KK> return O;
 KK> }
The functions are correct but the comments aren't. The function does _not_
create a stream. (Nor it has to). It accepts a stream (by ref) as param and
returnd that same stream.
When you put:
cout << myMC;
the compiler invoke
operator << ( typeof(cout), typeof(myMC));
The same stream must be returned to take advantage of chaining, like
cout << myMC << " " << 1 << "\n";
that will output myMC to cout, then " " to the stream returned by the
previous function (cout too), then the same happens to all other elements.
Paul
... Anybody got a magnet?
--- OS/2 Warp
---------------
* Origin: The FlintStones' Cave in BedRock (2:371/20)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.