一段有疑问的代码。在VC++6.0下运行到Delete就是出错。
#include <iostream.h>
#include <string.h>
class word
{
char *str;
public:
word( char *s)
{ str=new char[strlen(s)+1];
strcpy( str,s );
}
char &operator[](int k)
{ return *(str+k); }
void disp()
{ cout<<str<<endl; }
~word()
{
delete str;
}
};
void main()
{
char *s="china";
word w(s);
w.disp();
int n=strlen(s);
while (n>=0)
{
w[n-1]=w[n-1]-32;
n--;
}
w.disp();
}
while (n>=0)
{
w[n-1]=w[n-1]-32;
n--;
}
while (n >= 1/*0*/)
{
w[n-1] = w[n-1] - 32;
n--;
}