class sstring
{
public:
sstring(const char *p);
sstring(const sstring &s);
~sstring();
sstring &operator =(const sstring &s);
const char * c_str();
private:
char *str;
};this
sstring::sstring(const char * p):str(nullptr)
{
if (p==nullptr)
{
str = new char[1];
if(str != nullptr)
str = '\0';
}
else
{
int length = strlen(p);
str = new char[length + 1]; // 若能加 NULL 判斷則更好
if(str != nullptr)
strcpy_s(str, length + 1,p);
}
}string
sstring::sstring(const sstring & s):str(nullptr)
{
int length = strlen(s.str);
str = new char[length + 1];
if(str)
strcpy_s(str, length + 1, s.str);
}class
sstring::~sstring()
{
if (str != nullptr)
{
delete str;
str = nullptr;
}
}co
sstring & sstring::operator=(const sstring & s)
{
if (this != &s)
{
delete[] str;
size_t length = strlen(s.str);
str = new char[length + 1];
if(str)
strcpy_s(str, length + 1, s.str);
}
return *this;
}let
const char * sstring::c_str()
{
return str;
}new