Say for a class String I am overloading the operator += to add to the string (append) an object of that class and a normal string.
My definitions would go:
String operator+=(String&);
String operator+=(const char*);
And the code would be:
String String::operator+=(String &other) /* adds another object*/
{
data = (char *)realloc(data,(strlen(data)+strlen(other.data))*sizeof(char)+1);
return strcat(data,other.data);
}
String String::operator+=(const char* other)
{
data = (char *)realloc(data,(strlen(data)+strlen(other))*sizeof(char)+1);
return strcat(data,other);
}
Examine these codes.
Now for iostreams:
The declarations:
friend ostream& operator << ( ostream&, String& ) ;
friend istream& operator >> ( istream&, String& ) ;
The corresponding codes:
ostream& operator << ( ostream &strm, String &x )
{
strm<
return strm ;
}
// friend function to input objects of string class
istream& operator >> ( istream &strm, String &x )
{
char *buffer ;
buffer = new char[256] ;
if ( strm.get( buffer, 255 ) )
x = String ( buffer ) ;
return strm ;
}