1
0
Fork 0
This repository has been archived on 2021-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
uni-m.cds-adv-prog/ex4.3/String.cc

31 lines
625 B
C++
Raw Normal View History

2019-12-17 15:05:27 +01:00
//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 ;
}