C++封装一个string类(顺带说一下为什么operator为什么一般放在内外实现)

代码实现:

#include <iostream>
#include <cstring>

using namespace std;

class Mystring
{
    char* my_date;
public:
    Mystring()
    {
        my_date=new char[1];
        my_date[0]='\0';
    }

    Mystring(const char* str)
    {
        int len=strlen(str);

        my_date=new char[len+1];
        memcpy(this->my_date,str,len);

        this->my_date[len]='\0';
    }

    Mystring& operator=(const Mystring& other)
    {
        if(this==&other){
            return *this;
        }

        if(this->my_date!=nullptr){
            delete []this->my_date;
            this->my_date=nullptr;
        }

        int len=strlen(other.my_date);
        this->my_date=new char[len+1];
        mempcpy(this->my_date,other.my_date,len);
        my_date[len]='\0';

        return *this;
    }


    int get_len()
    {
        return strlen(this->my_date);
    }

    char operator[](int index)
    {
        if(index<0 || index>this->get_len()){
            cout<<"越界访问";

            return -1;
        }

        return this->my_date[index];
    }

    Mystring& operator+(const Mystring& other)
    {
        strncat(this->my_date,other.my_date,strlen(other.my_date));

        return *this;
    }


    char* get_my_date()const
    {
        return this->my_date;
    }

 

};

ostream& operator<<(ostream& cout,const Mystring& other)
{
    cout<<other.get_my_date();

    return cout;
}

int main()
{
    Mystring a="i love China";
    cout<<a<<endl;
    return 0;
 }

如果放在内中实现就只能像下面这种方式进行调用,并不是我们想要的那种效果,所以一般用的比较少:

#include <iostream>
#include <cstring>
#include <cstdlib>

using namespace std;

class Mystring
{
    char* my_date;
public:
    Mystring()
    {
        my_date=new char[1];
        my_date[0]='\0';
    }

    Mystring(const char* str)
    {
        int len=strlen(str);

        my_date=new char[len+1];
        memcpy(this->my_date,str,len);

        this->my_date[len]='\0';
    }

    Mystring& operator=(const Mystring& other)
    {
        if(this==&other){
            return *this;
        }

        if(this->my_date!=nullptr){
            delete []this->my_date;
            this->my_date=nullptr;
        }

        int len=strlen(other.my_date);
        this->my_date=new char[len+1];
        mempcpy(this->my_date,other.my_date,len);
        my_date[len]='\0';

        return *this;
    }


    int get_len()
    {
        return strlen(this->my_date);
    }

    char operator[](int index)
    {
        if(index<0 || index>this->get_len()){
            cout<<"越界访问";

            return -1;
        }

        return this->my_date[index];
    }

    Mystring& operator+(const Mystring& other)
    {
        strncat(this->my_date,other.my_date,strlen(other.my_date));

        return *this;
    }


    char* get_my_date()const
    {
        return this->my_date;
    }


    ostream& operator<<(ostream& cout)
    {
        cout<<get_my_date();

        return cout;
    }

};



int main()
{
    Mystring a="asdas";
    a.operator<<(cout);
    a<<(cout);    
    return 0;
 }

猜你喜欢

转载自blog.csdn.net/a2998658795/article/details/126210833