目的,实现自定义类型字符串,部分运算符重载。
#include
#include<string.h>
#include
class String
{
public:
String(){}
String(char* ptr)
{
mptr = new charstrlen(ptr) + 1;
strcpy_s(mptr, strlen(ptr) + 1, ptr);
}
const String operator +(const char *source)
{
char * tmp = new char[strlen(mptr)+strlen(source) + 1]();
strcpy_s(tmp, strlen(mptr) + 1,mptr);
strcat(tmp,source);
String strtmp(tmp);
return strtmp;
}
bool operator <(const String& rhs)
{
return strcmp(mptr,rhs.mptr) < 0;
}
bool operator != (const String& rhs)
{
return strcmp(mptr,rhs.mptr)!=0;
}
char& operator[](int i)
{
return mptr[i];
}
~String()
{
delete[] mptr;
mptr = NULL;
}
private:
char* mptr;
friend const String operator +(const char * ,const String& );
friend std::ostream& operator <<(std::ostream& out,const String& rhs);