reinterpret-cast

Should I use static_cast or reinterpret_cast when casting a void* to whatever

Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a good reason to favor one over the other? ...

Is reinterpreting a member function pointer a 'good idea' ?

I have a worker thread, which holds a list of 'Thread Actions', and works through them as an when. template <class T> class ThreadAction { public: typedef void (T::*action)(); ThreadAction(T* t, action f) : func(f),obj(t) {} void operator()() { (obj->*func)(); } void (T::*func)(); T* obj; }; It's normally called like...

C++: Safe way to cast an integer to a pointer

I need to convert an integral type which contains an address to the actual pointer type. I could use reinterpret_cast as follows: MyClass *mc1 = reinterpret_cast<MyClass*>(the_integer); However, this does not perform any run-time checks to see if the address in question actually holds a MyClass object. I want to know if there is any b...

casting via void* instead of using reinterpret_cast

Hi, I'm reading a book and I found that reinterpret_cast should not be used directly, but rather casting to void* in combination with static_cast: T1 * p1=... void *pv=p1; T2 * p2= static_cast<T2*>(pv); instead of: T1 * p1=... T2 * p2= reinterpret_cast<T2*>(p1); However, I can't find an explanation why is this better than the dire...

Problem using reinterpret_cast<> in c++

I am trying to cast a datastream into a struct since the datastream consists of fixed-width messages and each message has fulle defined fixed width fields as well. I was planning on creating a struct and then using reinterpret_cast to cast pointer to the datastream to the struct to get the fields. I made some test code and get weird resu...

Why doesn't this reinterpret_cast compile?

I understand that reinterpret_cast is dangerous, I'm just doing this to test it. I have the following code: int x = 0; double y = reinterpret_cast<double>(x); When I try to compile the program, it gives me an error saying invalid cast from type 'float' to type 'double What's going on? I thought reinterpret_cast was the rogue cast t...

Need clarifications in C-style, reinterpret, and const casts

Am I right in assuming that C-style casts (which are discouraged) are nothing but reinterpret_casts? Using the latter is visually striking and easy to search when looking for nasty casts, and hence it's recommended over C-style casts? If casting away const using const_cast and writing to a originally const object is undefined, what is t...

What wording in the C++ standard allows static_cast<non-void-type*>(malloc(N)); to work?

As far as I understand the wording in 5.2.9 Static cast, the only time the result of a void*-to-object-pointer conversion is allowed is when the void* was a result of the inverse conversion in the first place. Throughout the standard there is a bunch of references to the representation of a pointer, and the representation of a void poi...

Way to get unsigned char into a std::string without reinterpret_cast?

I have an unsigned char array that I need in a std::string, but my current way uses reinterpret_cast which I would like to avoid. Is there a cleaner way to do this? unsigned char my_txt[] = { 0x52, 0x5f, 0x73, 0x68, 0x7e, 0x29, 0x33, 0x74, 0x74, 0x73, 0x72, 0x55 } unsigned int my_txt_len = 12; std::string my_std_string(reinterpret_c...

Cast from Void* to TYPE* using C++ style cast: static_cast or reinterpret_cast

So if your converting from Void* to Type* or from Type* to Void* should you use: void func(void *p) { Params *params = static_cast<Params*>(p); } or void func(void *p) { Params *params = reinterpret_cast<Params*>(p); } To me static_cast seems the more correct but I've seen both used for the same purpose. Also, does the dir...

C++ cast to array of a smaller size

Here's an interesting question about the various quirks of the C++ language. I have a pair of functions, which are supposed to fill an array of points with the corners of a rectangle. There are two overloads for it: one takes a Point[5], the other takes a Point[4]. The 5-point version refers to a closed polygon, whereas the 4-point versi...

C++ reinterpret_cast

Hi, I don't know why this simple code is not working. Can someone please explain me? int main() { const char* c = "ret"; typedef unsigned char GOK_UINT8; typedef GOK_UINT8* pGOK_UINT8; const pGOK_UINT8 y = reinterpret_cast<const GOK_UINT8*>(c); return 0; } Can someone tell me why the reinterpret_cast should no...

question about reinterpret_cast

i have following code #include <iostream> using namespace std; int main(){ int i; char *p="this is a string"; i=reinterpret_cast<int>(p); cout<<i<<"\n": return 0; } output is: 7648 please explain reinterpret_cast ...

reinterpret_cast cast cost

My understanding is that C++ reinterpret_cast and C pointer cast is a just a compile-time functionality and that it has no performance cost at all. Is this true? ...

Class Private members modified on creating a structure (C++)

I was just going through some codes of C++. Where in I came across the concept of reinterpret_cast operator. EDIT 1 : I know that accessing private members of a class is not recommended. But in some situations we ought to go ahead and access them. I have just put forth this question to get my concepts clear. In the example that I re...