C++自己实现一个String类(构造函数、拷贝构造函数、析构函数和字符串赋值函数)
#include <iostream> #include <cstring> using namespace std; class String{ public: // 默认构造函数 String(const char *str = nullptr); // 拷贝构造函数 String(const String &str); // 析构函数 ~String(); // 字符串赋值函数 String& operator=(const String &str); private: char *m_data; int m_size; }; // 构造函数 String::String(const char *str) { if(str == nullptr) // 加分点:对m_data加NULL 判断 { m_data = new char[1]; // 得分点:对空字符串自动申请存放结束标志' '的 m_data[0] = '