Original Post
I did a quick Google search so I could quickly review the C++ cast operators. In the search results was an www.informit.com article. Without having really read much from that site before, I had assumed it was a decent quality resource (something like Dr. Dobb's Journal). The article was titled "Why I Hate C++ Cast Operators". I rarely appreciate articles with such drastic statements since very few people in software engineering or computer science have the credibility to be so bold (see: Bjarne Stroustrup). Always wanting to learn more I read the article anyways. The article is utter shit. The part where I officially lost my mind was the example he gave for why C++ cast operators are in fact "dangerous"! (&d); is the cast that a user would end up with -- and is indeed entirely wrong. Whats worse is that he immediately uses that bull crap example to justify why int n= int (d); is the superior notation. How did this get published? How did nobody email them? This is madness. I have long insisted that the internet should not be the last stop for information. I'll typically use it as a starting point, then I will immediately use a bound and well reviewed book. I will use select internet resources from or recommended by trusted individuals. However overall, the internet is a shit hole, and I'm not only talking about goatse. [Edited by - fpsgamer on October 7, 2007 9:00:17 PM]
Quote:First of all, the first line is explicit and self documenting but most importantly it is the correct cast to use and does compile. END OF EXAMPLE. Regardless, the author continues the example with a series of nonsensical operations that a fictional newbie would attempt next. Eventually he decides that *reinterpret_cast
Excerpt from: Why I Hate C++ Cast Operators by Danny Kalev / www.informit.com --- int n; double d=15.95; int n= static_cast (d); This code will not compile, though. Our naïve programmer doesn’t give up. He decides to modify the code a bit: int n= reinterpret_cast(d); Surprise, surprise. This version wouldn’t compile either. As a last resort, our poor programmer uses the following: int n= *reinterpret_cast(&d); This version compiles without a problem. Our programmer expects that after the cast operation, n shall be 15. Alas, it’s actually 1717986918! The reasons for this aren’t very important at the moment; what’s important is that many C++ programmers do believe that the cast expression above should initialize n to 15, which it doesn’t. Lo and behold, C-style cast does get it right.