C++ String类的简单实现(非模板)

头文件

#ifndef MY_STRING_H
#define MY_STRING_H

class Mstring
{

private:
    int m_length;
    char* m_pointer;

public:
    //构造函数
    Mstring();
    //有参构造
    Mstring(const char* str);
    //拷贝构造
    Mstring(const Mstring& obj);
    //获取长度
    int length()const;
    //转换为C字符串
    const char* c_str();

    //操作符的重载
    char& operator[](int index)const;
    Mstring& operator = (const Mstring& obj);
    const Mstring operator + (const Mstring& obj)const;
    bool operator > (const Mstring& obj)const;
    bool operator < (const Mstring& obj)const;
    const Mstring& operator += (const Mstring& obj);
    bool operator == (const Mstring& obj)const;
    bool operator != (const Mstring& obj)const;

    //获取自身
    Mstring& self();

    //析构函数,销毁数组
    ~Mstring();

};

#endif // MY_STRING_H

函数实现

#include "My_string.h"
#include <cstring>

Mstring::Mstring():m_length(10)
{
    m_pointer = new char[m_length];
    strcpy(m_pointer, "");
}

Mstring::Mstring(const char* str)
{
    m_length = strlen(str);
    m_pointer = new char[m_length];
    strcpy(m_pointer, str);
}

Mstring::Mstring(const Mstring& obj)
{
    m_length = obj.m_length;
    m_pointer = new char[m_length];
    memcpy(m_pointer, obj.m_pointer, m_length);
}

int Mstring::length()const
{
    return m_length;
}

char& Mstring::operator[](int index)const
{
    return m_pointer[index];
}

const char* Mstring::c_str()
{
    return m_pointer;
}

Mstring& Mstring::operator = (const Mstring& obj)
{
    //防止自赋值
    if(this != &obj)
    {
        char* pointer = new char[obj.m_length];

        if( pointer )
        {
            memcpy(pointer, obj.m_pointer, obj.m_length);

            m_length = obj.m_length;
            delete m_pointer;
            m_pointer = pointer;
        }

    }

    return *this;
}

const Mstring Mstring::operator + (const Mstring& obj)const
{
    Mstring ret;
    //释放构造函数申请的空间
    delete ret.m_pointer;

    ret.m_length = m_length + obj.m_length;
    ret.m_pointer = new char[ret.m_length];

    if( ret.m_pointer )
    {
        strcpy(ret.m_pointer, m_pointer);
        strcat(ret.m_pointer, obj.m_pointer);
    }

    return ret;
}

bool Mstring::operator > (const Mstring& obj)const
{
    return (strcmp(m_pointer, obj.m_pointer) > 0) ? 1 : 0;
}

bool Mstring::operator < (const Mstring& obj)const
{
    return (strcmp(m_pointer, obj.m_pointer) < 0) ? 1 : 0;
}

const Mstring& Mstring::operator += (const Mstring& obj)
{
    char* pointer = new char[m_length + obj.m_length];

    if( pointer )
    {
        strcpy(pointer, m_pointer);
        strcat(pointer, obj.m_pointer);

        m_length = m_length + obj.m_length;
        delete m_pointer;
        m_pointer = pointer;
    }

    return *this;
}

bool Mstring::operator == (const Mstring& obj)const
{
    return (strcmp(m_pointer, obj.m_pointer) == 0) ? 1 : 0;
}

bool Mstring::operator != (const Mstring& obj)const
{
    return (strcmp(m_pointer, obj.m_pointer) != 0) ? 1 : 0;
}

Mstring& Mstring::self()
{
    return *this;
}

Mstring::~Mstring()
{
    delete[]m_pointer;
}

效果图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值