#include <assert.h>
class String
{
public:
String(const char*) ;
String(const String&);
String& operator=(const String&);
~String();
void Clone(const String&);
private:
char* buffer_;
};
String::String(const char* s)
{
if (!s)
{
buffer_ = new char[1];
assert(buffer_ != NULL);
buffer_[0] = '\0';
}
else
{
Clone(s);
}
}
String::String(const String& rhs)
{
Clone(s);
}
String& String::operator=(const String& rhs)
{
if (this == &rhs )
{
return *this;
}
delete [] buffer_;
Clone(s);
return *this;
}
String::~String()
{
if (buffer_)
{
delete [] buffer_;
buffer_ = NULL;
}
}
void String::Clone(const String& rhs)
{
buffer_ = new char[strlen(rhs.buffer_)+1];
assert(buffer_ != NULL);;
strcpy(buffer_, rhs.buffer_);
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交