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 

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

效果图

C++ String类的简单实现(非模板)_第1张图片

你可能感兴趣的:(c++,开发语言)