#ifndef _MYSTING_h_
#define _MYSTING_h_
/*
String类;
*/
using namespace std ;
/************************************************************************/
/*访函数,好处:相当于闭包,使得函数能够使用绑定的局部变量(成员变量)
用于函数重载时比较清晰
其它(待补充)*/
/************************************************************************/
class Strlen_c
{
public:
int operator()( const char * cs)
{
if( cs == NULL)
return -1;
int len =
-1;
while ( len ++,* cs ++
!= ‘\0‘);
return len;
}
//int operator()(const char *cs,const char
*wu);
};
class MyString
{
friend ostream&
operator <<( ostream & out , const MyString & string);
friend istream&
operator >>( istream &in, MyString & string);
public:
MyString();
MyString( const char * cs);
MyString( const MyString & ms);
~ MyString();
MyString&
operator =( const MyString & ms);
MyString&
operator=( const char * cs);
MyString operator +( const MyString & ms);
MyString&
operator +=( const MyString & ms);
char operator[]( const int index);
int getSize() const
/************************************************************************/
/*关于const成员函数的几点说明:
const 对象只能访问const成员
const成员函数访问数据成员时隐含的this指针const化*/
/************************************************************************/
{
return this-> m_size;
}
char* getStr() const
{
return this-> m_str; //返回值为一个指针常量;
}
private:
char * m_str;
Strlen_c m_strlen;
int m_size;
};
#endif
cpp实现:
#include "stdafx.h"
MyString::MyString ()
{
m_str = new char[1];
m_str = ‘\0‘;
m_size =
0;
}
MyString::MyString (const char *cs )
{
if ( cs == NULL)
return;
m_size = m_strlen( cs);
m_str = new char[ m_size+1];
char * strTmp = m_str;
while
((* strTmp ++
= * cs ++)
!= ‘\0‘);
}
MyString::MyString (const MyString &ms )
{
if ( ms. getSize()
> m_strlen( m_str))
{
m_size = ms. getSize();
delete m_str;
m_str = new char[ m_size+1];
}
char * mstmp = ms. getStr();
char * mtmp = m_str;
while((* mtmp ++ =
* mstmp ++)
!= ‘\0‘);
}
MyString::~MyString ()
{
delete m_str;
m_str = NULL;
}
MyString& MyString ::operator=(const MyString &ms )
{
if( m_size < ms. getSize())
{
m_size = ms. getSize();
delete m_str;
m_str = new char[ m_size+1];
}
char * mstmp = ms. getStr();
char * mtmp = m_str;
while ((* mtmp ++ =
* mstmp ++)
!= ‘\0‘);
return * this;
}
MyString& MyString ::operator=(const char *cs )
{
if( m_size < m_strlen( cs))
{
m_size = m_strlen( cs);
delete m_str;
m_str = new char[ m_size +1];
}
const char * cstmp = cs;
char * mtmp = m_str;
while((* mtmp ++ =
* cstmp ++)
!= ‘\0‘);
return * this;
}
MyString MyString ::operator+(const MyString &ms )
{
int len = m_size + ms. getSize();
char * newtmp = new char[ m_size +1];
char * mstmp = ms. getStr(),* mtmp = m_str,* tmp = newtmp;
while((* tmp ++ =
* mtmp ++)
!= ‘\0‘);
tmp --;
while((* tmp ++ =
* mstmp ++)
!= ‘\0‘);
/*MyString newString(newtmp);
//关于内存释放,做成成员变量等都能实现,但我想不想那么写,大神建议用vector,但那样就是去了这么些的意义
delete
newtmp;
//希望somebody能帮助解决这一问题;
return
newString;*/
//只能暂且以为返回局部变量是坚决不允许的;
return newtmp;
}
MyString& MyString ::operator+=(const MyString & ms)
{
* this =
* this + ms;
return * this;
}
char MyString ::operator[](const int index)
{
if ( index < 0
|| index >= m_size)
return ‘\0‘;
return m_str[ index];
}
ostream&
operator <<(ostream &out , const MyString &string )
{
out << string. getStr();
return out;
}
/************************************************************************/
/*
关于输入流长度的问题暂时没有好的方法解决,
等待以后有能力解决,或者大神们赐教吧*/
/************************************************************************/
istream&
operator >>(istream &in ,MyString &str )
{
//in.read(str.getStr(),4);//读取4个字节到str中;
in. width(100); //限制宽度
char tmpstr[100];
in >> tmpstr;
str = tmpstr;
return in;
}
至于其它的一些重载没有什么代表性,就不一一写出了;