42 lines
1.2 KiB
C
42 lines
1.2 KiB
C
|
#ifndef __MYSTRING_H__
|
||
|
#define __MYSTRING_H__
|
||
|
|
||
|
#include <iostream>
|
||
|
#include <cstring>
|
||
|
|
||
|
// 重载: 实现自定义字符串类
|
||
|
class MyString
|
||
|
{
|
||
|
friend ostream &operator<<(ostream &cout, const MyString &str) {}
|
||
|
friend istream &operator<<(istream &cout, MyString &str) {}
|
||
|
|
||
|
public:
|
||
|
MyString(const char *str); // 有参构造函数
|
||
|
MyString(const MyString &str); // 拷贝构造函数
|
||
|
~MyString(); // 析构函数
|
||
|
|
||
|
public:
|
||
|
// 字符串拼接
|
||
|
MyString &operator+(MyString &other); // 重载 + 运算符
|
||
|
MyString &operator+(const char *other); // 重载 + 运算符
|
||
|
|
||
|
// 字符串赋值
|
||
|
MyString &operator=(MyString &other); // 重载 = 运算符
|
||
|
MyString &operator=(const char *other); // 重载 = 运算符
|
||
|
|
||
|
// 字符串比较
|
||
|
MyString &operator==(MyString &other); // 重载 == 运算符
|
||
|
MyString &operator==(const char *other); // 重载 == 运算符
|
||
|
|
||
|
// 读取字符串中 index 位置的字符
|
||
|
char operator[](int index); // 重载 [] 运算符
|
||
|
|
||
|
private:
|
||
|
char *mStr; // 字符串
|
||
|
int mSize; // 字符串长度
|
||
|
};
|
||
|
|
||
|
ostream &operator<<(ostream &cout, const MyString &str);
|
||
|
istream &operator<<(istream &cout, MyString &str);
|
||
|
|
||
|
#endif
|