G
GrahamJWalsh
I have a basic question regarding various data types (related to
reinterpret_cast, static_cast).
Lets say I have some declarations thus;
int i = 444;
unsigned int* uip;
I then do something like;
// 1.
// compiler error.... can't cast from int* to unsigned int*
//uip = static_cast<unsigned int*> (&i);
//2.
// this will work and uip == 444
uip = reinterpret_cast<unsigned int*> (&i);
//3.
// this will give me i3 == 666 (I know that if i2 <0 i3 will be
// rubbish)
int i2=666;
unsigned int i3;
i3=static_cast<int> (i2);
Could anybody tell me whats going on under the covers such that 1 fails
and 2 succeeds. I guess the question is what do the templates
static_cast and reinterpret_cast do at a low level.
Why does the static_cast in 1. fail when using pointers but succeed in
3. when I'm not dealing with pointers.
I reckon my question is basically, what sort of checking do static_cast
and reinterpret_cast do wrt their parameters and what are the rules.
thanks much for any info. have a nice day.
G
reinterpret_cast, static_cast).
Lets say I have some declarations thus;
int i = 444;
unsigned int* uip;
I then do something like;
// 1.
// compiler error.... can't cast from int* to unsigned int*
//uip = static_cast<unsigned int*> (&i);
//2.
// this will work and uip == 444
uip = reinterpret_cast<unsigned int*> (&i);
//3.
// this will give me i3 == 666 (I know that if i2 <0 i3 will be
// rubbish)
int i2=666;
unsigned int i3;
i3=static_cast<int> (i2);
Could anybody tell me whats going on under the covers such that 1 fails
and 2 succeeds. I guess the question is what do the templates
static_cast and reinterpret_cast do at a low level.
Why does the static_cast in 1. fail when using pointers but succeed in
3. when I'm not dealing with pointers.
I reckon my question is basically, what sort of checking do static_cast
and reinterpret_cast do wrt their parameters and what are the rules.
thanks much for any info. have a nice day.
G