头文件
#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;
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
函数实现
#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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
效果图