C++ Faq-lite (Pointer/Reference casts are evil? No.)
Perusing through the C++ Faq-lite, searching for the page to describe a problem to a friend, I come across some references to pointer casting being "evil". First off, I think this is one of the most comprehensive and well written faqs on the web, but I'm going to have to go ahead and really seriously disagree with this one point.
He argues the evilness of other things such as arrays and #defines. I'm inclined to agree on the #defines, and moderatly to a few of his points on arrays. But this casting of types to new types being a problem is simply ridiculous.
The concept of a feature of C++ being potentially "dangerous" is in no way an argument against it, IMHO. Perhaps one could suggest that container classes are easier than arrays, and type casting should be avoided, when talking to someone _very_ new to C++. But personally I would recomend the practice in using them. How else will one learn? These concepts are intrinsic to the language, and their understanding imparts more than simply the ability to use them.
I mean, pointer casting (especially to void and back) is rather essential. In some instances I can think of no other way to accomplish certain tasks... And its just plain easier and faster for others. Are we stupid or something? Nobodys going to cast 'foo*' to 'bar*' unless they're both derived from type 'gibble'. And if you do, you're an idiot, and its not the fault of the feature you're using. Theres a million ways to seriously fsk up your app. Why single out this one, simply because its easy to do?
If I say
Is it the fault of the delete operator, or am I just plain stupid? ... Exactly. ;)
P.S. Yes, I'm aware that I finally post after months of almost straight silence, and its this irrelevant piece of drivel... What can I say? :p
Re: C++ Faq-lite (Pointer/Reference casts are evil? No.)
Concerning c++ (NOT c), in very rare cases pointer casting might be found useful, but most often, it's a quick solution for a design flaw. In an OOP C++ program, specifically void pointer casting should never have to be used (and yes, this is one of the few times that I actually will say never).
If you find yourself "having to do so" then you are probably missing a relationship between the types you are casting between (inheritance). Of course, this is assuming that you are using oop, which isn't necissarily true. If you are not an oop programmer, then your use of casting is more understandable.
Originally posted by lightatdawn
I mean, pointer casting (especially to void and back) is rather essential. In some instances I can think of no other way to accomplish certain tasks...
But if your programs truely are OOP and "properly" designed (yeah, I know, I don't like saying that word either), I can't think of very many times you'd have to pointer cast between types. The only situtations I can think of are things such as binary fileio, or enumerating a child type from a list and casting it such that you can access all of its information (and even here the datatypes are related via inheritance). There are probably more times, but these are what I can think of off the top of my head. Pointer casting is not an elegant solution, but should generally be looked at as a "quick fix" for a design flaw. Again, it may not always be a design flaw, but if you find youself having to do it often, then you should think twice about it because it probably is.