c++ string的实现。

第三次做了。只是做个复习。偶然发现之前的版本有内存泄露。基本功还是不过关。这次应该没有内存泄漏了。虽然是个简单版本。

1)了解堆,栈,值copy。

2)几个常用的c的字符函数和c中的char 如何表示串。和c++的string不同。

3)string。自动有‘’,  。 "hi.",这样一个常字符串,编译器也是会给''的。char [3]={xxx,''} 必须自己加。

main.cpp

#include <iostream>
#include "Mystring.h"
using namespace std;

void main_mystring();
int main()
{
    main_mystring();

    return 0;
}

//mystring g_hi("hia");
//void main_mystring()
//{
//    mystring a;
//    mystring b("hi");
//    mystring c=mystring("linson");
//    mystring d=c;
//    d=b;
//    d[2]='a';
//}

void main_mystring()
{
    Mystring hi("hi");
    Mystring hi2="h2";
    Mystring c=hi;
    c=c;
    cout<<c<<endl;

    c[1]='x';

    cout<<c<<endl;

    Mystring emptystr;
    cout<<emptystr<<endl;
    emptystr=hi;
    cout<<emptystr<<endl;

    Mystring add=hi+hi2;
    cout<<add<<endl;

}


Mystring.h
#ifndef MYSTRING_H_INCLUDED
#define MYSTRING_H_INCLUDED

#include <stdio.h>
#include <iostream>
using namespace std;


class Mystring
{
public:
    Mystring(char* const);
    Mystring();
    Mystring(const Mystring&);
    Mystring& operator=(const Mystring&);
    char& operator[](unsigned int);
    Mystring operator+(const Mystring& right);
    ~Mystring();
private:
    Mystring(char * const,unsigned int);//专给+操作符使用.之前的版本应该内存泄漏了.
    unsigned int CharSize;
    char* pChar;
friend ostream& operator<<(ostream& os,const Mystring& mys);
};

ostream& operator<<(ostream& os,const Mystring& mys);



#endif // MYSTRING_H_INCLUDED
Mystring.cpp
#include "Mystring.h"
#include "malloc.h"
#include "string.h"
#include <stdexcept>

using namespace std;

Mystring::Mystring(char* const _pchar)
{
    int Length=0;
    char* pflag=_pchar;

if(_pchar==0)
{
  throw runtime_error("null pointer");
}

while(*pflag!=0x0 && Length<1024*1024)////strlen还是感觉不安全.如果本来形参就是一个没有结尾的符号呢.长度怕出错.随便假设最大为1m长度的字符串吧.
    {
        ++Length;
        ++pflag;
    }
    pChar=(char *)malloc(Length+1);
    //cout<<"new"<<(void *)pChar<<endl;
    if(pChar!=0)
    {
        strncpy(pChar,_pchar,Length);
    }
    else
    {
        throw runtime_error("alloc error.");
    }
    CharSize=Length;
    pChar[CharSize]='';

}

Mystring::Mystring(const Mystring& source)
{
    pChar=(char *)malloc(source.CharSize+1);
    if(pChar==0)
    {
        throw runtime_error("alloc error.");
    }
    //cout<<"new"<<(void *)pChar<<endl;
    strncpy(pChar,source.pChar,source.CharSize);
    CharSize=source.CharSize;
    pChar[CharSize]='';
}

Mystring::Mystring()
{
    pChar=(char *)malloc(1);
    //cout<<"new"<<(void *)pChar<<endl;
    if(pChar==0)
    {
        throw runtime_error("alloc error.");
    }
    CharSize=0;
    pChar[CharSize]='';

}

Mystring& Mystring::operator=(const Mystring& source)
{
  string temp=string(source);   
  std::swap(pChar,temp.pChar);
return *this;
    return *this;
}

char& Mystring::operator[](unsigned int index)
{
    if(index>=0&& index<CharSize)
    {
        return pChar[index];
    }
    else
    {
        throw runtime_error("over range!");
    }
}

Mystring Mystring::operator+(const Mystring& right)
{

    char * temppChar=(char *)malloc(this->CharSize+right.CharSize+1);
    //cout<<"new"<<(void *)temppChar<<endl;
    if(pChar==0)
    {
        throw runtime_error("alloc error.");
    }
    strncpy(temppChar,this->pChar,this->CharSize);
    strncpy(temppChar+this->CharSize,right.pChar,right.CharSize);
    int tempCharSize=this->CharSize+right.CharSize;
    temppChar[tempCharSize]='';
    return Mystring(temppChar,tempCharSize);
    //Mystring()
}

Mystring::Mystring(char * const _p,unsigned int _size)
{
    pChar=_p;
    CharSize=_size;
}

Mystring::~Mystring()
{
    //cout<<"del"<<(void *)pChar<<endl;
    free(pChar);
}


ostream& operator<<(ostream& os,const Mystring& mys)
{
    return os<<mys.pChar;
}
原文地址:https://www.cnblogs.com/lsfv/p/5677291.html