一个强壮的C++类

mystring - 源码文件 - 点击下载

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值