简单实现—Mystring

      书写一个属于我们的spring类,C++相对于C而言引入了string的数据类型,对于C++来讲一切皆对象,那C++中的string便是一个类,对于string str;str便是string中的一个对象,是linux中书写的一个类。接下来我们便自己书写一个属于自己的string类,mystring类。

         首先:创建一个mystring类,类中的私有属性有char *str,公共方法有构造               数 Mystring(),Mystring(const char *str1),析构函数~Mystring()  string+:Mystring operator+(Mystring &str1);string+=:mystring &operator+=(const mystring &str2) string ==:bool operator==(const mystring &str2) string=:operator=(mystring str2);

1:创建对象时无参数传递进来;

2:创建对象时有参数传递进来,此时对对象str指针进行开辟空间与参数大小加一的空间后面有'\0'

3:析构函数,用于销毁构造函数里面开辟的空间 

 

4: 实现string+:如string str3,string str1;string str2:str3=str1+str2;>>对象str3= 对象str1+对象str2,,str2是不会改变的所以是const mystring &str2;首先先为str3开辟一片空间,使可以装下str1+str2. 运算符重载函数operator; 返回值为str3,str1为当前对象,str2为传递的参数,+运算符重载的函数为:mystring this.operator+(const mystring & str2);

 

 5:实现string+=;string str1("hello"),string str2("word");str1+=str2; this. operator+=(const mystring & str2); 赋值函数会涉及深拷贝,浅拷贝问题

 

 6:实现string ==;bool this.operator==(const mystring &str2);

 

 7:实现string =:不能当前对象的指针直接指向str2.str。这样只是浅拷贝。浅拷贝问题是对于指针属性赋值只是指向通一片空间,但每个析构函数都会销毁一次,造成一个空间被销毁两次,所以执行代码报错double free

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你实现一个简单mystring类。 mystring类的定义如下: ```c++ class mystring { public: // 构造函数 mystring(); mystring(const char* str); mystring(const mystring& other); // 析构函数 ~mystring(); // 成员函数 size_t length() const; const char* c_str() const; mystring& operator=(const mystring& other); mystring& operator+=(const mystring& other); char& operator[](size_t index); const char& operator[](size_t index) const; // 友元函数 friend bool operator==(const mystring& lhs, const mystring& rhs); friend bool operator!=(const mystring& lhs, const mystring& rhs); friend bool operator<(const mystring& lhs, const mystring& rhs); friend bool operator<=(const mystring& lhs, const mystring& rhs); friend bool operator>(const mystring& lhs, const mystring& rhs); friend bool operator>=(const mystring& lhs, const mystring& rhs); friend mystring operator+(const mystring& lhs, const mystring& rhs); private: char* m_data; // 字符串数据 size_t m_len; // 字符串长度 }; ``` 下面是类成员函数的实现: ```c++ #include <cstring> #include <iostream> class mystring { public: // 构造函数 mystring() : m_data(nullptr), m_len(0) {} mystring(const char* str) : m_len(std::strlen(str)) { m_data = new char[m_len + 1]; std::strcpy(m_data, str); } mystring(const mystring& other) : m_len(other.m_len) { m_data = new char[m_len + 1]; std::strcpy(m_data, other.m_data); } // 析构函数 ~mystring() { delete[] m_data; } // 成员函数 size_t length() const { return m_len; } const char* c_str() const { return m_data; } mystring& operator=(const mystring& other) { if (this != &other) { delete[] m_data; m_len = other.m_len; m_data = new char[m_len + 1]; std::strcpy(m_data, other.m_data); } return *this; } mystring& operator+=(const mystring& other) { char* new_data = new char[m_len + other.m_len + 1]; std::strcpy(new_data, m_data); std::strcpy(new_data + m_len, other.m_data); delete[] m_data; m_data = new_data; m_len += other.m_len; return *this; } char& operator[](size_t index) { return m_data[index]; } const char& operator[](size_t index) const { return m_data[index]; } // 友元函数 friend bool operator==(const mystring& lhs, const mystring& rhs) { return std::strcmp(lhs.m_data, rhs.m_data) == 0; } friend bool operator!=(const mystring& lhs, const mystring& rhs) { return std::strcmp(lhs.m_data, rhs.m_data) != 0; } friend bool operator<(const mystring& lhs, const mystring& rhs) { return std::strcmp(lhs.m_data, rhs.m_data) < 0; } friend bool operator<=(const mystring& lhs, const mystring& rhs) { return std::strcmp(lhs.m_data, rhs.m_data) <= 0; } friend bool operator>(const mystring& lhs, const mystring& rhs) { return std::strcmp(lhs.m_data, rhs.m_data) > 0; } friend bool operator>=(const mystring& lhs, const mystring& rhs) { return std::strcmp(lhs.m_data, rhs.m_data) >= 0; } friend mystring operator+(const mystring& lhs, const mystring& rhs) { mystring result; result.m_len = lhs.m_len + rhs.m_len; result.m_data = new char[result.m_len + 1]; std::strcpy(result.m_data, lhs.m_data); std::strcpy(result.m_data + lhs.m_len, rhs.m_data); return result; } private: char* m_data; // 字符串数据 size_t m_len; // 字符串长度 }; ``` 上面的实现中,我们使用了C++标准库中的函数`std::strlen`、`std::strcpy`和`std::strcmp`。这些函数分别用于计算字符串长度、复制字符串和比较字符串。由于这些函数都是C语言标准库中的函数,因此它们在C++中也是可用的。 你可以使用下面的代码测试一下mystring类的功能: ```c++ #include <iostream> #include "mystring.h" int main() { mystring s1 = "hello"; mystring s2 = "world"; std::cout << "s1: " << s1.c_str() << std::endl; std::cout << "s2: " << s2.c_str() << std::endl; std::cout << "s1 length: " << s1.length() << std::endl; std::cout << "s2 length: " << s2.length() << std::endl; std::cout << "s1 + s2: " << (s1 + s2).c_str() << std::endl; std::cout << "s1 == s2: " << (s1 == s2) << std::endl; std::cout << "s1 != s2: " << (s1 != s2) << std::endl; std::cout << "s1 < s2: " << (s1 < s2) << std::endl; std::cout << "s1 <= s2: " << (s1 <= s2) << std::endl; std::cout << "s1 > s2: " << (s1 > s2) << std::endl; std::cout << "s1 >= s2: " << (s1 >= s2) << std::endl; s1 += s2; std::cout << "s1 += s2: " << s1.c_str() << std::endl; return 0; } ``` 输出结果如下: ``` s1: hello s2: world s1 length: 5 s2 length: 5 s1 + s2: helloworld s1 == s2: 0 s1 != s2: 1 s1 < s2: 1 s1 <= s2: 1 s1 > s2: 0 s1 >= s2: 0 s1 += s2: helloworld ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值