2017解放军最新宣传片:为什么有0,请看注释

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/27 20:25:36
#include<iostream.h>
class A{
int a;
public:
A(int x=0){a=x;}
int geta(){return a;}
operator char*()
{
int n=1,t=a;
while(t=t/10)n++;
char* s=new char[n+1];
for(int i=n,t1=a;i>=0;i--)
{
s[i]=t1%10+'0';//为什么必须有‘0’?
t1=t1/10;
}
s[n]='\0';
return s;
}
};
void main()
{
A a(12345);
cout<<a.geta()<<endl;
char *str;
str=(char*)(a);
cout<<str<<endl;
delete str;
}

把数字转成ASSIC码.'0'在这里被看着ASSIC值48,而不是0.

如果不加0,t1%10的值就会被默认转成ASSIC值,其实其并不能表示正确的数字.