istream & operator>>(istream & is,mystring & s)//重载>>
{
int n = 10;
int group=1;
int i=0;
char ch;
delete [] s.pstr;
s.pstr = new char[n];
ch = is.get();
while(ch!='\n')
{
s.pstr[i] = ch;
i++;
ch = is.get();
if(i==group*n-1)
{
group++;
s.pstr = (char *)realloc(s.pstr,group*n);
}
}
s.pstr[i] = '\0';
return is;
}
new应该和delete成对出现。realloc底层调用free,而非delete。强烈不推荐你这样写。你可以将new换成malloc。而且realloc完后不能直接用,应该判断一下返回值是否有效。
首先你这里18行之后应该break吧,否则反复realloc干啥呢。
mystring类内部析构需要释放pstr