31 lines
625 B
C++
31 lines
625 B
C++
|
//String.cc
|
||
|
#include "String.hh"
|
||
|
|
||
|
String operator+( const String& lhs, const String& rhs ) {
|
||
|
String result(lhs); // Copy lhs into res with constructor
|
||
|
result += rhs ; // Use operator+= function which is a member of the class
|
||
|
return result ;
|
||
|
}
|
||
|
|
||
|
String& String::operator=( const String& a ) {
|
||
|
if ( &a != this ) {
|
||
|
insert(a._s) ;
|
||
|
}
|
||
|
|
||
|
return *this ;
|
||
|
}
|
||
|
String& String::operator+=( const String& a ) {
|
||
|
int newlen = _len + a._len ;
|
||
|
char * newstr = new char[ newlen+1 ] ;
|
||
|
|
||
|
strcpy(newstr, _s) ;
|
||
|
strcpy(newstr + _len, a._s) ;
|
||
|
|
||
|
if (_s) delete[] _s ;
|
||
|
|
||
|
_s = newstr ;
|
||
|
_len = newlen ;
|
||
|
|
||
|
return *this ;
|
||
|
}
|