mystring.h
#ifndef MYSTRING_H
#define MYSTRING_H
class mystring
{
private:
static mystring *self;
char *s;
public:
~mystring();
static mystring *makestring(const char *s = NULL);
static void deletestring();
const char *get_s() const;
void set_s(const char *s);
protected:
mystring();
mystring(const char *s);
mystring(const mystring &it);
};
#endif // MYSTRING_H
mystring.cpp
#include <iostream>
#include <string.h>
#include "mystring.h"
mystring *mystring::self = NULL;
mystring::mystring():s(NULL)
{
//cout << "mystring::mystring()" << endl;
}
mystring::mystring(const char *s)
{
//cout << "mystring::mystring(const char *s)" << endl;
int len =strlen(s);
this->s = new char[len+1];
strcpy(this->s,s);
this->s[len] = 0;
}
//拷贝函数
mystring::mystring(const mystring &it)
{
//cout << "mystring::mystring(const mystring *it)" << endl;
int len =strlen(it.get_s());
this->s = new char[len+1];
strcpy(this->s,it.get_s());
this->s[len] = 0;
}
mystring::~mystring()
{
//cout << "mystring::~mystring()" << endl;
delete []s;
}
//单例创建函数
mystring *mystring::makestring(const char *s)
{
//cout << "mystring *mystring::makestring(const char *s)" << endl;
if(self == NULL){
if(s == NULL){
self = new mystring();
}
else{
self = new mystring(s);
}
}
return self;
}
//单例删除删除函数
void mystring::deletestring()
{
//cout << "void mystring::deletestring()" << endl;
if(self != NULL){
delete self;
self = NULL;
}
}
//字符串去提取函数
const char *mystring::get_s() const
{
//cout << "char *mystring::get_s()" << endl;
//printf("s Memory address is %p\n",&this->s);
if(this->s != NULL){
return s;
}
return "fales";
}
//字符串设置函数
void mystring::set_s(const char *s){
//cout << "void mystring::set_s(const char *s)" << endl;
if(this->s != NULL){
delete []this->s;
}
int len = strlen(s);
this->s = new char[len+1];
strcpy(this->s,s);
this->s[len] = 0;
}
main.cpp
#include <iostream>
#include <mystring.h>
#include <string.h>
using namespace std;
int main()
{
//mystring str1("wunanhui");
//mystring str2 = str1;
//cout << "str1 " << str1.get_s() << " lenth is " << strlen(str1.get_s()) << endl;
//cout << "str2 " << str2.get_s() << " lenth is " << strlen(str2.get_s()) << endl;
//str1.set_s("wuchanzhuwuchanzhuwuchanzhu");
//str2.set_s("wuchanzhu");
//cout << "str1 " << str1.get_s() << " lenth is " << strlen(str1.get_s()) << endl;
//cout << "str2 " << str2.get_s() << " lenth is " << strlen(str2.get_s()) << endl;
mystring *str3 = mystring::makestring();
cout << "str3 " << str3->get_s() << " lenth is " << strlen(str3->get_s()) << endl;
str3->set_s("wuchanzhu");
cout << "str3 " << str3->get_s() << " lenth is " << strlen(str3->get_s()) << endl;
str3->set_s("wuchanzhuwuchanzhuwuchanzhu");
cout << "str3 " << str3->get_s() << " lenth is " << strlen(str3->get_s()) << endl;
mystring::deletestring();
mystring *str4 = mystring::makestring("wunanhui");
cout << "str4 " << str4->get_s() << " lenth is " << strlen(str4->get_s()) << endl;
mystring *str5 = mystring::makestring("wuchanzhu");
cout << "str5 " << str5->get_s() << " lenth is " << strlen(str5->get_s()) << endl;
mystring::deletestring();
delete str3;
delete str4;
delete str5;
return 0;
}