栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

代码实现:
#include 
#include 

using namespace std;

class Mystring
{
    char* my_date;
public:
    Mystring()
    {
        my_date=new char[1];
        my_date[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]='';
    }

    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]='';

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

using namespace std;

class Mystring
{
    char* my_date;
public:
    Mystring()
    {
        my_date=new char[1];
        my_date[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]='';
    }

    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]='';

        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< 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1041213.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号