TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: BRIAN S
from: HERBERT BUSHONG
date: 1997-10-28 11:55:00
subject: write

::> #include 
::> class myoutput {
::>   public:
::>     void operator<< (char* s)
::>       {
::>         cout << s;
::>       }
::> };
::> I would like to be able to call my << function multiple times like the 
ou
::> class.  for example :
::> write << "Hello " << "World";
::> Can anyone help?
1) Make your operator<< function a friend of ostream, or 
2) derive your class from ostream   (cout is an instance of an ostream)
#1 
class myoutput
{
   ...rest of class...
   public:
   friend ostream& operator<<(ostream &os, char *s)
       {
           os << s;
           return os;
       }
};
Notice we return the stream reference so we can do multiple operations:
myoutput mout;
mout << "Hello" << "World!";
#2
class myoutput : public ostream
{
  ...rest of class...   
}
That's it. Notice that no operator<< needed to be defined for #2. It inherits
the function from ostream and you would only need to overload it if you 
wanted 
different output.
#  Herbert Bushong    harchon@centuryinter.net       [TEAM OS/2]    
-  Blackbeard's BBS   Intelec: 239:600/0            
+  Fido: 1:19/19      http://www.intelec.com/software/
---
   RM 1.31 2508   Good thing I'm wearing brown shoes...
---------------
* Origin: Blackbeard's BBS - Ville Platte, LA - 318-468-3385 (1:19/19)

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™.