By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

c++ and data types

P: n/a
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

Jul 27 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Gr**********@gmail.com wrote:
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


static_cast asks the compiler to convert a value of one type to a value
of another type according to well defined rules. Rules are defined in
the standard for how to convert each built in type to another built in
type. You can extend the set of rules to user defined types in a few
different ways. You can define a constructor (without the explicit
keyword) that takes a type, example:

class A {
public:
A(int x) {}
} ;
A a = static_cast<A>(3) ;
You also define operators, example:
class A
{
public :
operator int()
{ return 42 ; }
} ;

A a ;
int x = static_cast<int>(a) ;
Of course, for the above examples, casting isn't actually needed at all,
but the goal here is to try to explain what static_cast is doing. It is
looking for some rule to convert the type you give it to the type you
say it should be. If there isn't some well defined rule for doing so,
the compiler considers it an error.

Now, as far as the compiler is concerned, "int" and "unsigned int" are
two different types. While the conversion between the two is well
defined, a _pointer_ to one type has no business pointing to an object
of the other type. In fact, about the only time the compiler can safely
allow a pointer to one type to point to an object of a different type is
when a base class pointer is pointing to an object of a derived class.
So, since the compiler doesn't have any rules for converting an "int *"
to an "unsigned int *", it decides that the cast must be an error.

reinterpret_cast, on the other hand, is your mechanism for telling the
compiler that you really do know what you are doing and you really do
want to treat an object of one type as an object of another. The
standard doesn't make many guarantees about what actually happens to a
value when you do a reinterpret_cast. It might change, or it might not.
The only real guarantees you have are that if you cast a pointer to
an integer type, and then cast the integer back to a pointer of the same
type, then the pointer value is unchanged, and that the result of
casting a null pointer is a null pointer of the target type.

-Alan
Jul 27 '05 #2

P: n/a
purrfect! thanks so much for that response alan. Cleared it up for me
nicely. have a nice day

GrahamO

Jul 27 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.