c ++でのreinterpret_cast 強制型変換
使用法:
reinterpret_cast<类型>(变量)
注(次の内容は、コンパイラによって異なる場合があります)
#include<iostream>
using namespace std;
int main()
{
int intarray[5]={1,2,3,4,5};
int *intptr=intarray;
cout<<"cout: "<<'\n';
for(int i=0;i<5;i++)
{
cout<<*intptr<<" at "<<reinterpret_cast<unsigned long>(intptr) <<'\n';
intptr++;
}
return 0;
}
オンラインコースの例は上記のとおりですが、いくつか変更があります!しかし、フォーカスは同じまま
ですが、次のように、devc ++で実行するとエラーが報告されました
[Error] cast from 'int*' to 'long unsigned int' loses precision [-fpermissive]
考えられる理由は、バイト数が異なり、
int *を強制的に変換できないことが原因であると推定されています。8バイトで、変換後は4バイトになる可能性があるため、精度が失われます
代わりに以下を試してください
reinterpret_cast<unsigned long long>(intptr)
成功!!!
out出:
cout:7339504に
1つ7339508に
2つ7339512に
3つ7395516に
4つ7339520に
5つ7339520に