string 类的功能
#include<iostream>#include<cstring>
using namespace std;
class mystring
{
public :
mystring (char *n);
mystring (int m);
mystring ();
mystring (mystring &Mytring);
mystring &operator = (mystring &Mystring);
mystring &operator + (mystring &p);
void display();
~mystring();
private :
char *N;
int M;
};
mystring::mystring(char *n)
{
int len = sizeof(n);
N = new char[len];
strcpy(N, n);
}
mystring::mystring(int m)
{
N = new char[m + 1];
for (int i = 0; i < m; i++)
N[i] = ' ';
N[m] = '\0';
}
mystring :: mystring (mystring &Mystring ){
M = Mystring.M;
N = new char [M+1];
strcpy(N,Mystring.N);
}
mystring::mystring()
{
M = 1;
N = new char[M];
N[0] = '\0';
}
mystring &mystring::operator = (mystring &Mystring){
M = Mystring.M;
delete []N;
N = new char [M+1];
strcpy(N,Mystring.N);
return *this;
}
mystring &mystring::operator + (mystring &p)
{
char *NN;
int MM = M;
NN = new char[MM];
strcpy(NN, N);
delete []N;
N = new char[MM + p.M + 1];
strcpy(N, NN);
strcat(N, p.N);
N[MM + p.M] = '\0';
delete[]NN;
return *this;
}
void mystring::display()
{
cout<<"<";
if (N != NULL)
cout<<N;
cout<<">"<<endl;
}
mystring::~mystring()
{
delete []N;
}
int main()
{
mystring s1 ("sdfgfdgdf"), s2(5), s3, s4("nzx"), s5("liu"), s6;
s1.display();
s2.display();
s3.display();
s3 = s1;
s3.display();
s6 = s4 + s5;
s6.display();
return 0;
}
这是什么原因啊? 我是想编写一个能实现关于string 功能的类函数 可是老是出现这个问题 求大神帮忙看一下 , 小弟感激不尽