String字符串类实现

1、
class String
{
public:
    String(const char *str = null);//普通构造函数
    String(const String& other);//拷贝构造函数
    ~String();//析构函数
    String& operator = (const String& other);//赋值函数
    String(String&& other);//移动构造
    String& operator = (String&& other);//移动赋值

private:
    char *m_data;
};
2、构造函数
String::String(const char *str)
{
    if(str == NULL)
    {
        m_data = new char[1];
        *m_data = '\0';
    }
    else
    {
        m_data = new char[strlen(str)+1];
        strcpy(m_data,str);
    }
}
string str("hell0");
3、析构函数
String::~String()
{
    delete [] m_data;
}
4、拷贝构造
String::String(const String &other)
{
    m_data = new char[strlen(other.m_data)+1];
    strcpy(m_data,other.m_data);
}
string s1("hello");
string s2=s1;
5、拷贝赋值
String& String::operator=(const String &other)
{
    //检查自赋值
    if(this == &other)
        return *this;
    delete[] m_data;//释放原有内存资源
    m_data = new char[strlen(other.m_data)+1];
    strcpy(m_data,other.m_data);
    return *this;//返回本对象的引用
}
string s1("hello");
string s2;
s2 = s1;
6、移动构造
String(String&& other):m_data(nullptr)
{
    m_data = other.m_data;
    other.m_data = nullptr;
}
//或者借用move()
String(String&& other):m_data(std::move(other.m_data))
{
} 
7、移动赋值
String& operator = (String&& other)
{
    if(this!=&other)
    {
        delete[] m_data;
        m_data = other.m_data;
        other.m_data = nullptr;
    }
    return *this;
}
//或者借用move()
String& operator = (String&& other)
{
    m_data = move(other.m_data);
    return *this;
}
8、赋值操作符的
如果不加const的话:
string s3("pello");
const string s4("qello");
s3 = s4;
这样就会,因为一个 const 变量不能随意转化成非const变量

另外,
string s7("pello");
string s8("pellp");
string s9("qellp");
s9 = s7+s8;
不用const 会报错,因为 + 赋值必须返回一个操作值已知的string对象 ,除非他是一个const对象





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己实现字符串 class CMStringImp; class CMstring { public: explicit CMstring(void); ~CMstring(void); CMstring(LPCTSTR lpszstr); CMstring(const CMstring& lpszstr); CMstring& operator = (const CMstring& lpszstr); operator LPCTSTR() const; bool operator == (const CMstring&) const; bool operator != (const CMstring&) const; bool operator < (const CMstring&) const; TCHAR operator[] (int nIndex) const; TCHAR& operator[] (int nIndex); CMstring& operator += (LPCTSTR pStr); CMstring& operator += (TCHAR ch); friend CMstring operator+(const CMstring& str1, const CMstring& str2); friend CMstring operator+(const CMstring& str1, LPCTSTR lpszstr); friend CMstring operator+(const CMstring& str1, TCHAR ch); friend CMstring operator+(TCHAR ch, const CMstring& str1); friend CMstring operator+(LPCTSTR lpszstr, const CMstring& str1); // friend wostream operator <<(wostream &is;,const CMstring &str;); public: LPCTSTR GetData() const; bool IsEmpty() const; TCHAR GetAt(int) const; TCHAR& GetAt(int); int GetLength() const; int Compare(const CMstring&) const; int CompareNoCase(const CMstring&) const; int Find(TCHAR ch, int nStart = 0) const; int Find(LPCTSTR pStr, int nStart = 0) const; int ReverseFind(TCHAR ch) const; int ReverseFind(LPCTSTR pStr) const; CMstring Right(int nCount) const; CMstring Left(int nCount ) const; public: CMstring& MakeLower(); CMstring& MakeUpper(); CMstring& MakeReverse(); int Replace(TCHAR chOld, TCHAR chNew); int Replace(LPCTSTR pszOld, LPCTSTR pszNew); int Insert(int iIndex, TCHAR ch); void Format(LPCTSTR lpszFormat, ...); private: CMStringImp* m_pImp; };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值