测试reinterpret_cast的用法,如下:
#include <iostream>
using namespace std;void main()
{
int a;
float b;
int *p1;
float *p2; a = 3;
b = 4.5;
p1 = &a;
p2 = reinterpret_cast<float>(p1); cout<<"*p1= "<<*p1<<"\t"<<"*p2= "<<*p2<<endl;
}但是编译时,总是出错:error C2440: 'reinterpret_cast' : cannot convert from 'int *' to 'float'
There is no context in which this conversion is possible我看MSDN上的说明是,reinterpret_cast可以转换类型的指针的,可这里为什么不行呢?
#include <iostream>
using namespace std;void main()
{
int a;
float b;
int *p1;
float *p2; a = 3;
b = 4.5;
p1 = &a;
p2 = reinterpret_cast<float>(p1); cout<<"*p1= "<<*p1<<"\t"<<"*p2= "<<*p2<<endl;
}但是编译时,总是出错:error C2440: 'reinterpret_cast' : cannot convert from 'int *' to 'float'
There is no context in which this conversion is possible我看MSDN上的说明是,reinterpret_cast可以转换类型的指针的,可这里为什么不行呢?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货