reinterpret_cast problem

Started by
14 comments, last by derek6433 20 years, 8 months ago
quote:Original post by derek6433

variable_type bob = reinterpret_cast |_variable_type| FindVariableByName("bob");

[edited by - derek6433 on August 5, 2003 12:54:47 AM]


Why do I see "reinterpret_cast" used so much? What is the point? I never used it and I didn't run into problems.

Hey, don't forget to visit my page... by clicking here!

[edited by - FrancoisSoft on August 13, 2003 2:35:28 PM]
Advertisement
FrancoisSoft wrote ...
"Why do I see "reinterpret_cast" used so much? What is the point? I never used it and I didn''t run into problems."

If you have never used it, you are either using C-style casts, and that is _BAD_, or you have never come across a situation where you needed to use it.

In the OPs code he is converting void* to other variable types. As long as he is converting void pointers to some other pointers, a static_cast would be fine, but if he wanted to convert a void* to an int, for instance, static_cast wouldn''t work and he would have to use a reinterpret_cast.
Why do you think do static_cast, reinterpret_cast, dynamic_cast and const_cast look as ugly as they do? Because they want to remind you that you are doing something ugly. Besides some basic convertions (float to int e.g.), casts are almost always a bad idea and show a lack of good design. Casting each and everything from and to void * or being dependent on such EReturnDataType-nonsens slaps OOP in its face!!

What do you think, templates, virtual functions, design patterns and so on are good for? :D

Regards, VizOne
Andre Loker | Personal blog on .NET
Is there a difference between reinterpret_cast and C-style casting? Besides the fact that one was hit with the ugly stick too many times..
quote:Original post by psykr
Is there a difference between reinterpret_cast and C-style casting? Besides the fact that one was hit with the ugly stick too many times..


That's kind of the point. The new casts stick out like a sore thumb, so they're like a big red flag over potentially dodgy code. That's particularly useful with reinterpret_cast because it's non-portable. Also, they're much easier to search for than a typename in parentheses, which makes it easier to find reinterpret_casts when you're porting code.

The other good thing about them is that they are more specific, so it's harder to get the wrong cast by mistake.

[edited by - Krunk on August 13, 2003 7:10:40 PM]
If you know what type of variable you''ll be placing the data into, why do you need to find the type of the data pointed to by the pointer?



-Agent1

This topic is closed to new replies.

Advertisement