strcpy() 在里面的意思我有点糊涂了
#include<iostream>#include<string>
#include<cstring>
using namespace std;
class String
{
public:
String& operator+=(const String&);
String& operator+=(const char*);
int size() {return _size;}
char* c_str() {return _String;}
private:
int _size;
char* _String;
};
inline String& String::operator+=(const String &rhs)
{
if(rhs._String)
{
String tmp(*this);
_size = _size + rhs._size;
delete [] _String;
_String = new char[_size+1];
strcpy(_String,tmp._String);
strcpy(_String + tmp._size,rhs._String); // 这条语句 的意思 ....不太明白了...
}
return *this;
}
inline String& String::operator+=(const char *s)
{
if(s)
{
String tmp(*this);
_size = _size + strlen(s);
delete[] _String;
_String = new char[_size+1];
strcpy(_String,tmp._String);
strcpy(_String + tmp._size,s);
}
return *this;
};