C++为什么不这样重载>>
#include <iostream.h>
#include <fstream.h>
#include <string.h>
istream& operator>>(istream& is,char* &pstr)
{
char str[100];
is.get(str,100);
pstr=new char[strlen(str)+1];
if(pstr!=NULL)
strcpy(pstr,str);
}
void main()
{
char *p;
cin>>p;
cout<<p;
delete p;
p=0;
}
我开始的设想,只要str[]定义的足够大,那不就可以很好的完成cin>>p的作用,运行是出错,到MSDN查,发现标准库好像已经有这样的声明;
那为什么标准库不这样设计呢。岂不是很方便我们使用~