반응형
#include <string.h>
class String {
private:
char* _string;
int _length;
public:
String() : _string(NULL), _length(0) {}
String(const String& s) : _string(NULL), _length(0) {
_length = s._length;
if (_length > 0) {
_string = new char [s._length + 1];
strcpy (_string, s._string);
}
}
String(const char* s) : _string(NULL), _length(0) {
if (s != NULL) {
_length = strlen(s);
_string = new char [_length + 1];
strcpy (_string, s);
}
}
String(const char* s, int size) : _string(NULL), _length(0) {
if (s != NULL) {
_length = size;
_string = new char [_length + 1];
memcpy (_string, s, size);
_string[_length] = '\0';
}
}
String operator = (const String& s) {
if (this != &s) {
if (_string)
delete [] _string;
_length = s._length;
_string = new char [_length + 1];
strcpy (_string, s._string);
}
return *this;
}
~String() { delete [] _string; }
};
class String {
private:
char* _string;
int _length;
public:
String() : _string(NULL), _length(0) {}
String(const String& s) : _string(NULL), _length(0) {
_length = s._length;
if (_length > 0) {
_string = new char [s._length + 1];
strcpy (_string, s._string);
}
}
String(const char* s) : _string(NULL), _length(0) {
if (s != NULL) {
_length = strlen(s);
_string = new char [_length + 1];
strcpy (_string, s);
}
}
String(const char* s, int size) : _string(NULL), _length(0) {
if (s != NULL) {
_length = size;
_string = new char [_length + 1];
memcpy (_string, s, size);
_string[_length] = '\0';
}
}
String operator = (const String& s) {
if (this != &s) {
if (_string)
delete [] _string;
_length = s._length;
_string = new char [_length + 1];
strcpy (_string, s._string);
}
return *this;
}
~String() { delete [] _string; }
};
반응형