include <iostream>

include <string.h>

using namespace std;

class mystring{
public:

mystring(){
    _str = new char[1];
    *_str = '\0';
}
mystring(char *s){
    int len = strlen(s);
    _str = new char[len+1];
    strcpy(_str, s);
}
mystring(const char* s){
    if(s == nullptr){
        _str = new char[1];
    }else{
        _str = new char[strlen(s) + 1];
        strcpy(_str, s);
    }
}
//copy constructor
mystring(const mystring &another){
    _str = new char[strlen(another._str) + 1];
    strcpy(_str, another._str);
}
mystring &operator=(const mystring &another){
    if(this == &another){
        return *this;
    }
    delete []this->_str;
    int len = strlen(another._str);
    _str = new char[len+1];
    strcpy(_str, another._str);

    return *this;
}
mystring &operator+(const mystring &another){
    int catLen = strlen(this->_str);
    int srcLen = strlen(another._str);
    int len = catLen + srcLen;
    this->_str = static_cast<char *>(realloc(this->_str, len+1));
    memset(this->_str+catLen,0,srcLen+1);
    strcat(this->_str, another._str);

    return *this;
}
bool operator == (const mystring &another){
    return strcmp(this->_str, another._str) == 0;
}
bool operator > (const mystring &another){
    return strcmp(this->_str, another._str) > 0;
}
bool operator < (const mystring &another){
    return strcmp(this->_str, another._str) < 0;
}

char &operator[](int idx){
    return _str[idx];
}

~mystring(){
    delete [] _str;
}

private:

char *_str;

};

int main()
{

return 0;

}


JcTung
12 声望4 粉丝

如何成为一位架构师,需要非常扎实的知识基础!


« 上一篇
JAVA知识梳理
下一篇 »
C++指针与成员