Why does reinterperet cast work on pointers and static doesn't

Viewed 54

I'm learning about type casting and finding it pretty tough to understand it. So from what I've learned. You cannot static cast pointers of one type to another. I don't know exactly why. Because you can static cast an int to a char which has there own address in memory so its super confusing me as to why you cannot do the same with pointers.

So with reinterpret cast. You can convert a pointer type to another pointer type. I think?

So a few questions i have are. You can convert non pointer variables with a static cast.

And You can cast pointers types to different pointer types using reinterpret?

Below are some examples of stuff I'm struggling on.

<int *>something // what is the * inside of the <int> meaning? 

// i know what the previous *<int> means to deference.

// So if i have this does this mean I'm casting something into a char pointer
// and storing the cast inside of ptr?

char* ptr = static_cast<char *>something 

//With reinterpret cast i can cast from another type like this 

int x = 9;
int* ptr = {&x};

char* nowchar = reinterpret_cast<char *>&x

// the above is fine? 

//But i cannot do the same with static

int x = 8;
int* ptr = {&x};

char* nowchar = static_cast<char*>&x;


Can you explain why i cannot use static on a pointer, and why reinterpret_cast works

1 Answers

Taking this one question at a time:

<int *>something // what is the * inside of the <int> meaning?

When you are casting, what you put in the angle brackets is the type, int* (or int *) is a pointer to an integer. This is often confusing for those new to C++ because you also use the * operator to dereference a pointer, but in this context it denotes that the type is a pointer to an int and not an int.

// So if i have this does this mean I'm casting something into a char pointer
// and storing the cast inside of ptr?

char* ptr = static_cast<char *>something

Yes, the only case I'm aware of where this will work is if you are casting from / to a void* or if you are casting from a base class to a derived class, e.g.:

class Something{};
class SomethingElse : public Something{};

...

Something* something = new Something();
SomethingElse* ptr = static_cast<SomethingElse *>(something);

In this case, you are correct that you are casting the pointer to the instance of Something into a pointer for the derived class SomethingElse.

//With reinterpret cast i can cast from another type like this 

int x = 9;
int* ptr = {&x};

char* nowchar = reinterpret_cast<char *>(&x);

// the above is fine? 

//But i cannot do the same with static

int x = 8;
int* ptr = {&x};

char* nowchar = static_cast<char*>(&x);

In C++, static_cast is only used for a few (mostly) well-defined conversions. There is no well-defined conversion from a pointer to an int to a pointer to a char. Considering that neither the size (number of bits) of char nor int are specified, this makes sense. What does the compiler do if you are trying to cast a 16-bit int to an 8-bit char? In this case, what you need to do is reinterpret the int pointer to a char pointer; hence why you need to use a reinterpret_cast instead. Specifically, this will tell the compiler that it is allowed to reinterpret the underlying bit pattern of the data however it sees fit.

Related