c++ and data types

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
 
A

Alan Johnson

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
 
G

GrahamJWalsh

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

GrahamO
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top